Unobtrusive validation with dynamically loaded partial view

不问归期 提交于 2019-12-11 03:20:40

问题


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;


回答1:


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.



来源:https://stackoverflow.com/questions/22046168/unobtrusive-validation-with-dynamically-loaded-partial-view

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