Jquery to Check ModelState is Valid or not

10,695

For this I think you have two ways of working around. First you can simply make a ajax post to your controller (say:

public JsonResult IsModelStateValid(YourModel model)
{
  return Json(ModelState.IsValid);
}

and based on this you can do whatever you want. Second, if you don't want to make a server call, you can simply check

var isValid = $('#frm').valid()

in your jquery submit event or button click event. This will work if you have enabled unobtrusive validation and works for all in built validations of ASP.NET MVC, but not for your custom validation.

Share:
10,695
Praveen Kumar Vadige
Author by

Praveen Kumar Vadige

Updated on June 26, 2022

Comments

  • Praveen Kumar Vadige
    Praveen Kumar Vadige almost 2 years

    I want to check whether the ModelState is Valid or not through jquery. In My scenario if he click on submit button, and the fields fulfilled the requirement of Required Attributes, i want to open model popover, otherwise we want to show the errors, can any one tell me how to check ModelState is valid or not?

  • Praveen Kumar Vadige
    Praveen Kumar Vadige almost 8 years
    Where we want to enable Unobtrusive Validation??