How to verify that the email is already registered in the database?

Asked

Viewed 124 times

-1

The Controller is always returning the error message, when the email is registered or not in the database.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Nome,Email,Senha")] Pessoas pessoas)
{
    if (pessoas != null)
    {
        var verificaemail = db.Pessoas.Where(w => w.Email == "Email").FirstOrDefault();
        if (verificaemail != null)
        {                                        
            db.Pessoas.Add(pessoas);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ModelState.AddModelError("", "E-mail já cadastrado");
            return View();
        }
    }
    else
    {
        return View(pessoas);
    }  
}

2 answers

4

That?

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Nome,Email,Senha")] Pessoa pessoa) {
    if (pessoa != null) {
        if (db.Pessoas.Where(w => w.Email == pessoa.Email).FirstOrDefault() != null) {                                        
            db.Pessoas.Add(pessoa);
            db.SaveChanges();
            return RedirectToAction("Index");
        } else {
            ModelState.AddModelError("", "E-mail já cadastrado");
            return View();
        }
    } else return View(pessoa);
}

I put in the Github for future reference.

  • It is working, but with a however, is the opposite, when it was to give the error he register, and when it was to register is giving error. I guess my logic was wrong.

  • I did it based on what you posted and what you can infer, if I give some more information I can try to improve

  • If I change if (person != null) to (person == null) the logic of the code makes more sense?

  • I don’t think so, but I’d need to see more context.

4


You’re comparing whether the email matches the string "Email" while actually seeing if it’s what it contains in the person model in this way pessoas.Email

public ActionResult Create([Bind(Include = "Id,Nome,Email,Senha")] Pessoas pessoas)
{
    if (pessoas != null)
    {
        var verificaemail = db.Pessoas.Where(w => w.Email == pessoas.Email).FirstOrDefault();
        if (verificaemail != null)
        {                                        
            db.Pessoas.Add(pessoas);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            ModelState.AddModelError("", "E-mail já cadastrado");
            return View(pessoas);
        }
    }
    else
    {
        return View();
    }  
}
  • Detail, if 'people' is 'null' is to return 'View(people)'? .

  • 1

    Truth, well denoted I changed the answer

  • Now it is working, but with a however, it is the opposite, when it was to give the error he registers, and when it was to register is giving error. So I guess my logic is wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.