Data validation with mvc Asp.net (edition)

Asked

Viewed 1,007 times

0

I have a user registration where I use remote validation in the date Annotation to check if the user being registered already exists, my problem is in editing, as I am changing and using the same object the system thinks I am registering a new user and informs that I can not register because the informed record already exists.

How do I make sure that when I’m editing the record it doesn’t double check?

My model

            [DisplayName("E-mail")]
            [Required(ErrorMessage = "Favor informar o E-mail do usuário")]
            [Remote("Unico", "Usuario", ErrorMessage = "Esse e-mail já existe no sistema")]
            [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Favor informe um e-mail válido")]
            public string email{ get; set; }

And here I have the remote validation function

public ActionResult Unico(string email)
        {
            try
            {
                int idUsuario = 123;//Usuário/ADM de teste

                Models.user = bd.users.SingleOrDefault(s => s.email == email && s.idUsuario == idUsuario);
                bool retorno = false;
                if (c == null)
                {
                    retorno = true;
                }
                return Json(retorno, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }
  • 1

    You can put snippets of your code from Controller who makes those transactions mentioned in the question?

  • I edited including snippets of the code

  • 1

    Two things: MVC has a tag called [EmailAddress], that possibly validates even better the email than the regular expression used. The second thing is: where is the code that saves the record?

  • I have to confirm more I think that the MVC4 does not have the validation [Emailadress], not object problems nor data recording. my point is that when editing the user it informs that this email already exists in the bank by using remote validation.

2 answers

1

I’ve had that problem before, but I didn’t use the remote validation.

In my case, it occurred when receiving the object the ID was equal to zero. Thus, the persistence framework I was using understood that it was a new object to be registered.

Check that in your Action the registered user ID is received.

If not, render this ID in the View (in an Hidden input for example) so that it can be sent in the post to your Action.

If not, edit the question by adding your Controller code to try to help you.

0

RemoteAttribute expecting a JsonResult, and not a ActionResult. Anything other than true it interprets as an error. It may be a misinterpretation.

public JsonResult Unico(string email)
{
    try
    {
        int idUsuario = 123;//Usuário/ADM de teste
        Models.user = bd.users.SingleOrDefault(s => s.email == email && s.idUsuario == idUsuario);
        if (c == null)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        return Json(String.Format(CultureInfo.InvariantCulture,
            "O e-mail {0} já existe no sistema", email), JsonRequestBehavior.AllowGet);    
    }
    catch (Exception e)
    {
        return Json("Erro! " + e.Message, JsonRequestBehavior.AllowGet);
    }
}

With this you can simplify your [Remote], with more error messages until:

[Remote("Unico", "Usuario")]

There are more examples here: http://msdn.microsoft.com/en-us/library/gg508808(vs.98). aspx

Browser other questions tagged

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