Call MVC 3 Client Side Validation Manually for ajax posts

后端 未结 8 584
长发绾君心
长发绾君心 2020-11-30 22:16

I am creating an MVC 3 web application. I want to use Data Annotations on my entity class and then use unobtrusive client side validation before making a post back to the se

8条回答
  •  粉色の甜心
    2020-11-30 22:57

    At least in my case (MVC 5), it was also necessary to add the following code or else .valid() would always return true:

    $(function () {
    $(document).ajaxComplete(function(event, request, settings){
        //re-parse the DOM after Ajax to enable client validation for any new form fields that have it enabled
        $.validator.unobtrusive.parse(document);
    });
    

    });

    See http://johnculviner.com/the-unobtrusive-libraries-client-validation-on-ajax-in-asp-net-mvc-3/

提交回复
热议问题