I want to call “OnFailure” when ModelState is not valid in controller.
In My LoginView
@using (Ajax.BeginForm("Login", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "Login",InsertionMode = InsertionMode.Replace, OnSuccess = "Success", OnFailure = "onError" }))
{
}
In Controller
[httpPost]
public ViewResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
}
else
{
ModelState.AddModelError("login is fail")
}
return View("Login",model)
}
so i want call onSuccess Method if ModelState is valid and if it fail then call only OnError method with display all error which are in Model State.
Here’s what you could do:
and then all you need is the Success function:
in which you could test in which case you are:
By the way if you are using unobtrusive client side validation in the case of error where you are refreshing the form you will need to manually force the parsing of the new validation rules, otherwise next time you attempt to submit the form, client validation won’t work: