我目前面临的动态添加内容后验证的问题。
我有一个视图强类型的模型(顺序)。此订单可以有很多项目。模型看起来像下面这样:
public class Order { [Key] [HiddenInput] public int id { get; set; } [Display(Name = "Order Number")] public string number { get; set; } [Display(Name = "Order Date")] [DataType(DataType.Date)] [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:MM/dd/yyyy}")] public DateTime date { get; set; } [required(ErrorMessage = "Beneficiary is required.")] [Display(Name = "Beneficiary")] public int beneficiary_id { get; set; } [Display(Name = "Beneficiary")] public Beneficiary beneficiary { get; set; } [Display(Name = "Items")] public List<Item> items { get; set; } [Display(Name = "Payment Method")] public List<PaymentMethod> payment_methods { get; set; } }
我输入订单信息以及该特定订单的项目。我尝试了几种方式动态添加内容,最后去Steven Sanderson’s way。
在我看来,我有常规的订单信息,然后项目,我的模型看起来像这样:
@model trackmeMvc.Models.Model.Order @{ ViewBag.Title = "Create"; Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript(); } <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"></script> @using (Html.BeginForm("Create","Order",FormMethod.Post,new { @id = "create_order" })) { @Html.ValidationSummary(true,"Order creation was unsuccessful. Please correct the errors and try again.") <div class="editor-label"> @Html.LabelFor(m => m.date)<req>*</req> </div> <div class="editor-field"> @Html.TextBoxFor(m => m.date,new { @id = "order_date" })<br /> @Html.ValidationMessageFor(m => m.date) </div>
… …
<script type="text/javascript"> $(document).ready(function () { $("#addItem").click(function () { var formData = $("#main_div").closest("form").serializeArray(); $.ajax({ url: "/IPO/BlankItemRow",type: "POST",//data: formData,cache: false,success: function (html) { $("#editorRows").append(html); //$.validator.uobtrusive.parseDynamicContent("form *"); //$("#editorRows").removeData("validator"); //$("#editorRows").removeData("unobtrusiveValidation"); //$.validator.unobtrusive.parse("#editorRows"); //$.validator.unobtrusive.parse("#create_ipo"); //$.validator.unobtrusive.parseDynamicContent($(this).first().closest("form")); //$.validator.unobtrusive.parse($("#new_ipo_item")); //$.validator.unobtrusive.parseElement($("#editorRows").find(".editRow:last").children().find("select")); //$("#editorRows").find(".editRow:last").find("select").each(function () { //alert($(this).attr("id")); //$.validator.unobtrusive.parseElement($(this)); //$.validator.unobtrusive.parseDynamicContent($(this)); //$.validator.unobtrusive.parseDynamicContent($(this).attr("name")); //}); //$("#editorRows").children().find(".editRows:last").find("*").each(function () { // alert($(this).attr('id')); //$.validator.unobtrusive.parseDynamicContent('input'); //}); //var form = $(this).closest("form").attr("id"); //$(form).removeData("validator"); //$(form).removeData("unobtrusiveValidation"); //$.validator.unobtrusive.parse(form); } }); return false; }); }); </script>
这些是我尝试的一些东西,没有什么工作。
我得到了parseDynamicContent从Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC.我试过它在我可以想到的每一个场景,但仍然没有运气。
我也尝试了常规解析,从表单中删除验证,然后再次应用它,但仍然新添加的元素不验证:
<div id="editorRows"> @foreach (var item in Model.items) { @Html.Partial("_NewItem",item) } </div>
…和我的部分视图看起来像这样:
@model trackmeMvc.Models.Model.Item @{ Layout = ""; Html.EnableClientValidation(true); if (this.ViewContext.FormContext == null) { this.ViewContext.FormContext = new FormContext(); } } <div class="editRow"> @using (Html.BeginCollectionItem("order_items")) { @Html.DropDownListFor(m => m.item_id,@items,"None",new { @style = "width:205px;",@id = "ddlItems",@class="ddlItem",@name="ddlItemList" }) @Html.ValidationMessageFor(m => m.item_id) ... } </div>
所以发生了什么,我有一个空项目从控制器发送到视图默认情况下,显示一个空行。该项目已验证,但无论如何,当我单击添加项目,另一行出现,从该部分,但我不能得到它来验证。我试图把验证的局部视图,(在文档准备在主窗体)和我阅读的所有我应用,它总是结束相同:验证第一行,而不是其他。我试着验证史蒂文·桑德森为此目的 – 仍然没有运气 – 甚至验证的分数,发现at this link
和其后的页面是特定于部分验证…
我应该怎么做才能使验证工作?