[ACCEPTED]-Unobtrusive validation not working on dynamically-added partial view-dynamically-generated

Accepted answer
Score: 100

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

Before you call $.validator.unobtrusive.parse, remove the 3 original validator and unobtrusive validation 2 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 1 documented here.

Score: 4

What worked for me was to re-apply the validator 3 after the call to load the partial view. In 2 my case, I'm using $.post().then() but you could do something 1 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");
});

More Related questions