I have an AJAX post that does this
$.ajax({
type: "POST",
url: "@MyWebSite.Url/myController/myView",
contentType: "application/json; charset=utf-8",
data:
JSON.stringify({ myModel: myData }),
dataType: "json",
traditional: true,
success: function () {
alert('Success!');
},
error: function () {
alert('Error! ');
}
My controller does the validation check but it is not correctly returning the error message.
This is what my controller looks like
if (totalQty < part.QtyInItem)
{
//ModelState.AddModelError("", "There is " + part.QtyInItem + " of Part " + part.PartName + " used in item " + part.ItemID + " but you only handled " + totalQty + ". Please verify you are handling all parts used in the item.");
//RedirectToAction("myControler", myModel);
return this.Json(new { success = false, message = "There is " + part.QtyInItem + " of Part " + part.PartName + " used in item " + part.ItemID + " but you only handled " + totalQty + ". Please verify you are handling all parts used in the item." });
}
When I tried adding an error to the model state it just returned “ERROR!” and not the error message I had associated with it. And when I try doing the this.JSON return it returns “success” to the view and not the error message.
How can I do this validation check for my AJAX post
Go to Source
Author: ryan