unobtrusive jQuery validation on elements created by javascript MVC3

时光毁灭记忆、已成空白 提交于 2019-12-01 08:03:26

You need to parse the newly added contents in order to register it with client validation. And here's another blog post which discusses those issues. And yet another one.

I also had a similar scenario where I was loading content dynamically with Ajax using a Partial View. I was able to get my code working through a two step process.

  1. When the content is loaded to the DOM, I did the following:

    $.ajax({
       type: "Get",
       url: SomeURL,
       data: { TransactionId: recordId },
       success: function (data) {
          $('#SomeDiv').html(data);
          $.validator.unobtrusive.parse('#SomeDiv');              
          ShowModal();
       }
    });
    
  2. When submitting the form, I did the following:

    var form = $('#SomeDiv').find('form');
    if ($(form).valid()) {
       // call ajax post
    }
    

And that seemed to trigger unobtrusive validation. Hope someone gets something out of this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!