MVC3 Unobtrusive Validation Not Working after Ajax Call

后端 未结 5 721
既然无缘
既然无缘 2020-12-04 08:03

Ok, here is the deal, I have seen a few posts on SO relating to this issue, but nothing is working for me.

Basically, I have select drop downs that are being load

5条回答
  •  渐次进展
    2020-12-04 08:43

    I wrote this little snippet that will you can place in your javascript file and it will handle all your forms that are ajax loaded.

    //enable unobtrusive validation for ajax loaded forms
    $(document).ajaxSuccess(function (event, xhr, settings) {
        //process only if html was returned
        if ($.inArray('html', settings.dataTypes) >= 0) {
            //will parse the element with given id for unobtrusive validation
            function parseUnobtrusive(elementId) {
                if (elementId) {
                    $.validator.unobtrusive.parse('#' + elementId);
                }
            }
    
            //get the form objects that were loaded.  Search within divs
            //in case the form is the root element in the string
            var forms = $('form', '
    ' + xhr.responseText + '
    '); //process each form retrieved by the ajax call $(forms).each(function () { //get the form id and trigger the parsing. //timout necessary for first time form loads to settle in var formId = this.id; setTimeout(function () { parseUnobtrusive(formId); }, 100); }); } });

提交回复
热议问题