Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 Remote Validation set custom error message

Is there any way to set different errors according with logic in MVC 3.0 remote validation

public ActionResult IsUserEmailExists(string email)
        {
            bool isExists = service.IsUserExists(email);

            if(isExists )
             //Set error message
             return Json(!isExists, JsonRequestBehavior.AllowGet);
            else if(something)
              //another logic
              //Set errror message
              return Json(something, JsonRequestBehavior.AllowGet);             
        }

By default Remote validation using only ErrorMessage value from attribute declaration

[Remote("IsUserEmailExists", "Account", ErrorMessage = "User with such email already exists")]

is there any way to change that behavior?

like image 549
Joper Avatar asked May 14 '11 07:05

Joper


1 Answers

You could return the error message instead of a boolean value:

return Json("Some custom error message", JsonRequestBehavior.AllowGet);

In this case the model will be considered invalid (the same as if you had returned false) and the string used as error message.

like image 133
Darin Dimitrov Avatar answered Oct 17 '22 19:10

Darin Dimitrov