Unobtrusive validation doesn't work with Ajax.BeginForm

后端 未结 5 1663
醉梦人生
醉梦人生 2020-12-13 06:58

I have View with Model1 where I put Ajax.BeginForm() and in this View i have PartialView with Model2 where i put Ajax.BeginForm(). So only in first

5条回答
  •  死守一世寂寞
    2020-12-13 07:34

    That's because the second view is loaded with AJAX at a later stage and you need to call $.validator.unobtrusive.parse(...) immediately after its contents is injected into the DOM in order to enable unobtrusive validation. Look at the following blog post for more details.

    So in your case, instead of alerting in the OnSuccess callback of the first AJAX call, subscribe to a javascript function which will invoke this method:

    @using (Ajax.BeginForm(
        "Action1",
        "Controller",
        null,
        new AjaxOptions { 
            OnSuccess = "onSuccess",
            UpdateTargetId = "result"
        },
        null)
    )
    {
        
    }
    

    and then in your javascript file:

    var onSuccess = function(result) {
        // enable unobtrusive validation for the contents
        // that was injected into the 
    node $.validator.unobtrusive.parse($(result)); };

提交回复
热议问题