I m making jquery ajax post on a button click and doing unobtrusive form validation as below
$.validator.unobtrusive.parse('#formSite');
$('#formSite').validate();
if ($('#formSite').valid()) {
$.ajax({
type: "POST",
url: "/Controller/ActionMethod",
cache: false,
content: "application/html; charset=utf-8",
data: {
//data
},
success: function (data) {
if(data.success)
{
//processing
}else
{
//show errors
}
}
The above works perfectly fine for any existing model errors. In my controller I manually add errors in the modelstate and need to parse the form again showing those errors. How can we call $.validator.unobtrusive.parse('#formSite') with the additional server side errors and display them. My controller action method looks like this.
if (condition)
{
ModelState.AddModelError("PostCode", "Postcode District is invalid");
}
//processing
var modelErrors = AllErrors(ModelState);
return Json(new { success = false, modelErrors },JsonRequestBehavior.AllowGet);
$.validator.unobtrusive.parse('#formSite');does not do anything unless you first set the validator tonullanyway - user3559349<div>and update its contents with the error message. - user3559349