Unobtrusive validation not working on dynamically-added partial view

后端 未结 2 1004
一个人的身影
一个人的身影 2020-11-28 04:19

I am currently facing a problem with validation after dynamically adding content.

I have a view strongly typed to a model (Order). This Order can have m

相关标签:
2条回答
  • 2020-11-28 05:06

    Ok, I am going to start over with a new answer here.

    Before you call $.validator.unobtrusive.parse, remove the original validator and unobtrusive validation from the form like so:

    var form = $("#main_div").closest("form");
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);
    

    This same answer is documented here.

    0 讨论(0)
  • 2020-11-28 05:10

    What worked for me was to re-apply the validator after the call to load the partial view. In my case, I'm using $.post().then() but you could do something similar with a .always() callback of an AJAX call.

    $.post(url, model, function (data) {
        //load the partial view
        $("#Partial").html(data);
    }).then(function () {
        $("form").each(function () { $.data($(this)[0], 'validator', false); });
        $.validator.unobtrusive.parse("form");
    });
    
    0 讨论(0)
提交回复
热议问题