Sres
Sres

Reputation: 21

To check whether the email exists in database using Jquery

Here is my code. The GetByuserEmail takes the input email and checks with the database.If exists it returns the email.In checkmail() method the GetByUserEmail method is called and if the email exists then it should have returned true. Please check where might have gone wrong.

*public User GetByUserEmail(string email)
        {
            var users = db.Users.Where(u => u.Email == email);
            if (users.Count() > 0)
            {
                return users.First();
            }
            return null;
        }*


In regitster Controller,

*public JsonResult checkEmail(string email)
        {
            Models.User user = userRepository.GetByUserName(email);
            bool exists = (user != null);
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.Data = new { Exist = exists };
            return result;

        }*


It returns in json format

*{"Exist":false}*

Upvotes: 0

Views: 70

Answers (2)

Mike Devenney
Mike Devenney

Reputation: 1845

If Alexnadru's answer doesn't work try this:

var users = db.Users.Where(u => u.Email.ToLower() == email.ToLower());

Depending on your database settings [email protected] <> [email protected] (capitalization)

Upvotes: 0

Alexandru Pupsa
Alexandru Pupsa

Reputation: 1878

Shouldn't

Models.User user = userRepository.GetByUserName(email);

be

Models.User user = userRepository.GetByUserEmail(email);

?

EDIT: you are probably calling the wrong method on the first line from checkEmail method.

Upvotes: 1

Related Questions