部分视图中不显眼的客户端validation

我有一个部分视图,它在jQuery UI对话框中呈现。 因为它是动态内容,所以无法进行客户端validation是行不通的。 为了得到它,我必须强制validation器解析表单的内容,调用$.validator.unobtrusive.parse(); 。 但它不起作用。 我的浏览器报告未定义的不显眼对象。 为什么会这样? 也许jQuery库中有一些变化,现在整个事情的工作方式不同。 我正在使用jquery-1.6

您可能会发现以下博客文章很有用。

  (function($){ CampusCommon.register("Campus.UI.Popup") Campus.UI.Popup=function(){ defaults={ action:'', ispartialaction:'', customcallback:'', confirmaction:'', controltoupdateid:'', width:500, title:'', onsubmit:function(id){ var popupid=id+"_popupholder"; if(this.ispartialaction){ $.ajax({ url:this.action, type:"Get", context: this, data:$(this).find("form").serialize(), success:function(data){ $('#'+id).parents('body').find('form').append("
"); var ajaxContext=this; $("#"+popupid).dialog({ autoopen:false, model:true, width:this.width, title:this.title, buttons:{ "Confirm":function(){ if(ajaxContext.customcallback==''){ var popupform=$(this).find("form"); if(popupform.valid()){ $.post(ajaxContext.confirmaction,popupform.serialize(),function(d){ if(d!='') { $.each(d,function(i,j){ switch(j.Operation) { case 1: if($('#'+j.ControlClientID).is("select")) { $('#'+j.ControlClientID).val(j.Value); $('#'+j.ControlClientID).change(); } else if($('input[name="'+j.ControlClientID+'"]').length>0) { $('input[name="'+j.ControlClientID+'"][value="'+j.Value+'"]').prop("checked",true); } break; case 2: if($('#'+j.ControlClientID).is("select")) { $('#'+j.ControlClientID).append(""); } else { var len=$('input[name="'+j.ControlClientID+'"]').length; $('#'+j.ControlClientID+"list").append('
  • '); } break; case 0: $('#'+j.ControlClientID).val(j.Value); breakl default:break; } }); popupform.parent().dialog("destroy").remove(); $("#"+ajaxContext.controltoupdateid).change(); } }); } } else { executeByFunctionName(ajaxContext.customcallback,window,new Array()); } }, "Cancel":function(){ $(this).dialog("close"); } } }); $("#"+popupid).dialog("open"); $("#"+popupid).empty().append(data); }, error:function(e) { alert(e); } }); } else { var frm=document.createElement("form"); frm.id="CampusForm"; frm.name="CampusForm"; frm.action=this.action; frm.method="post"; var arr=$($("#"+id).closest("body").find("form")).serializeArray(); $.each(arr,function(i,j){ var hidd=document.createElement("input"); hidd.type="hidden"; hidd.name=j.name; hidd.value=j.value; frm.appendChild(hidd);}); document.appendChild(frm); frm.submit(); } } }, clicksubmit=function(){ var opts=$(this).data("CampusPopup"); opts.onsubmit($(this).attr("id")); return false; }; return { init:function(opt){ var opts=$.extend({},defaults,opt||{}); $(this).data('CampusPopup',opts); $(this).bind("click",clicksubmit); }}; }(); $.fn.extend({CampusPopup:Campus.UI.Popup.init}); })(jQuery) /*js*/ 1.7.1,1.5.1,validate,unobtrusive,8.20,common,popup.js [HttpGet] public ActionResult AddCourse(ViewModel.Batch batch) { return PartialView("~/Views/Admin/Course.cshtml", new ViewModel.Course()); } /*Layout*/ /*Batch*/ @model Campus.UI.Admin.Models.Batch @{ ViewBag.Title = "Batch"; }

    Batch

    @using (Html.BeginForm(Model.SubmitAction,Model.SubmitController)) { ViewContext.FormContext.ValidationSummaryId = "valSumId"; @Html.ValidationSummary(false, "Please fix these errors.", new Dictionary { { "id", "valSumId" } });
    Batch
    @Html.LabelFor(model => model.BatchName)
    @Html.EditorFor(model => model.BatchName)
    @Html.LabelFor(model => model.Courses)
    @Html.DropDownListFor(model => model.CourseId,Model.Courses,"--Select a course")
    @Html.LabelFor(model => model.BatchDescription)
    @Html.EditorFor(model => model.BatchDescription)

    } @section Scripts { @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/jqueryui") @Styles.Render("~/Content/themes/base/css") } /*Course*/ @model Campus.UI.Admin.Models.Course @{ ViewBag.Title = "Course"; }

    Course

    @using (Html.BeginForm()) { ViewContext.FormContext.ValidationSummaryId = "valSumId"; @Html.ValidationSummary(false, "Please fix these errors.", new Dictionary { { "id", "valSumId" } });
    Course
    @Html.LabelFor(model => model.CourseName)
    @Html.EditorFor(model => model.CourseName)
    @Html.LabelFor(model => model.CourseDescription)
    @Html.EditorFor(model => model.CourseDescription)

    }
    @Html.ActionLink("Back to List", "Index")
    @section Scripts { @Scripts.Render("~/bundles/jqueryval") }