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开发者_JAVA技巧(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?
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.
精彩评论