I used jquery.unobtrusive-ajax in my view. In this view, there is a form that is submit via ajax.
In the action if the modelsets is invalid, I will return the errors through the code below
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { errors = ModelState.Values.SelectMany(v => v.Errors) });
And in the view through the code below I show the errors to the user.
function failed(err1) {
obj = JSON.parse(err1.responseText).errors;
console.log(obj);
EmptyValidationErrors();
jQuery.each(obj, function (i) {
//alert(obj[i].ErrorMessage);
AddValidationError(obj[i].ErrorMessage);
ShowValidationErrors();
});
}
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data