Siz S
Siz S

Reputation: 866

Unobtrusive validation with dynamically loaded partial view

I'm loading partail view on button click

 function loadview(ele) {
        if (ele == 'account') {
           $('#updateprofile').load('@Url.Action("UpdateProfile", "Account")');
           $.validator.unobtrusive.parse($("#updateprofile"));
        }

        if (ele == 'password') {
           $('#changepassword').load('@Url.Action("ChangePassword", "Account")');
           $.validator.unobtrusive.parse($("#changepassword"));                
        }
   }

Validation is not working on partial view loaded by ajax request. However it works with @Html.Partial("ChangePassword", Model.changepassword)

Any help;

Upvotes: 2

Views: 1053

Answers (1)

iappwebdev
iappwebdev

Reputation: 5910

You have to call the parse function in the callback function of load:

function loadview(ele) {
    if (ele == 'account') {
        $('#updateprofile').load('@Url.Action("UpdateProfile", "Account")', function () {
           $.validator.unobtrusive.parse($("#updateprofile"));
        });
    }
    if (ele == 'password') {
        $('#changepassword').load('@Url.Action("ChangePassword", "Account")', function () {
           $.validator.unobtrusive.parse($("#changepassword"));
        });
    }
}

Right now, you are calling the parse function before any content could be loaded.

Upvotes: 6

Related Questions