unobtrusive validation not working with dynamic content

后端 未结 7 1668
一生所求
一生所求 2020-11-28 02:07

I\'m having problems trying to get the unobtrusive jquery validation to work with a partial view that is loaded dynamically through an AJAX call.

I\'ve been spending

相关标签:
7条回答
  • 2020-11-28 03:13

    If you try to parse a form that is already parsed it won't update

    What you could do when you add dynamic element to the form is either

    1. You could remove the form's validation and re validate it like this:

      var form = $(formSelector)
          .removeData("validator") /* added by the raw jquery.validate plugin */
          .removeData("unobtrusiveValidation");  /* added by the jquery unobtrusive plugin*/
      
      $.validator.unobtrusive.parse(form);
      
    2. Access the form's unobtrusiveValidation data using the jquery data method:

      $(form).data('unobtrusiveValidation')
      

      then access the rules collection and add the new elements attributes (which is somewhat complicated).

    You can also check out this article on Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC for a plugin used for adding dynamic elements to a form. This plugin uses the 2nd solution.

    0 讨论(0)
提交回复
热议问题