嗨,我正在使用MVC 3(剃须刀)来制作一个ajax向导.我注意到,当您将部分视图返回到UpdateTargetId时,可能会将视图插入,但不会添加/应用Unobtrusive
JavaScript.如果我将部分视图加载到ajax块外,例如
@H_502_2@@Html.Partial("Company")
它工作完美,所以我没有丢失任何标准的库和
我的网络配置是好的.
所以在这一刻,我很少受伤.
我的看法如下:
@H_502_2@@using(Ajax.BeginForm("Step","Origination",new AjaxOptions { UpdateTargetId = "stepArea" })){ <div id="stepArea"></div> <input id="btnSubmit" type="submit" value="submit" /> }控制器:
@H_502_2@public ActionResult Step(FormCollection formCollection) { if (this.Request.IsAjaxRequest()) { switch ((TempData["step"] as string)) { case "Company": TempData["step"] = "Person"; return PartialView("Company"); case "Person": TempData["step"] = "Pay"; return PartialView("Person"); case "Settlement": return PartialView("Pay"); default: TempData["step"] = "Company"; return PartialView("UserType"); } } return View(); }我的问题是从部分视图的验证可以从部分刷新中实现/实现?
解决方法
阅读几个论坛,做一些实验.
最后一块拼图,导致验证在返回部分视图后工作. jquery.validate.unobtrusive not working with dynamic injected elements @H_502_2@<script type="text/javascript"> function validateAjaxForm() { $("form").removeData("validator"); $("form").removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse("form"); return $('#form').valid(); } </script> @{ Html.EnableClientValidation(true); } @using (Ajax.BeginForm("Step",new AjaxOptions { UpdateTargetId = "stepArea",OnBegin = "return validateAjaxForm();" },new { id = "form" })) { <div id="stepArea"></div> <input id="btnSubmit" type="submit" value="submit" /> }
最后一块拼图,导致验证在返回部分视图后工作. jquery.validate.unobtrusive not working with dynamic injected elements @H_502_2@<script type="text/javascript"> function validateAjaxForm() { $("form").removeData("validator"); $("form").removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse("form"); return $('#form').valid(); } </script> @{ Html.EnableClientValidation(true); } @using (Ajax.BeginForm("Step",new AjaxOptions { UpdateTargetId = "stepArea",OnBegin = "return validateAjaxForm();" },new { id = "form" })) { <div id="stepArea"></div> <input id="btnSubmit" type="submit" value="submit" /> }
工作完美.