im having the following issue i have this post action that is called by ajax:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
if (ModelState.IsValid)
{
return Json(new { success = true });
}
else
{
return Json(new { success=false, error = true });
}
}
For other hand i have the following jquery handler for a button:
$("#Jquery_LoginButton").click(
function () {
$.ajax({
type: "POST",
url: "@(Url.Action("AX_Login","Users"))",
data:$("#MiniLoginForm").serialize(),
success: function (result)
{
alert("Good");
},
error: function ()
{
alert("Bad");
}
});
}
)
My Issue: The action is reached, but always the ajax call ends on sucess – even if the model is not valid -.
Questions:
-
Do you know why this is happening if im setting to false the success? i could throw an exception if the model is not valid but i dont see the thing elegant.
-
How do you manage normally the validation with Data Annotations and Ajax?
It is right that you always get “success” because your request
always work.
$.ajaxdoesnt know what data comes back.So you will get
erroronly if your request failes (404 for example)you have to do this
hope this helps