Note that there are some explanatory texts on larger screens.

plurals
  1. POUnobtrusive validation not working on dynamically-added partial view
    primarykey
    data
    text
    <p>I am currently facing a problem with validation after dynamically adding content.</p> <p>I have a view strongly typed to a model (<code>Order</code>). This Order can have many items. The model looks something like the following:</p> <pre><code>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&lt;Item&gt; items { get; set; } [Display(Name = "Payment Method")] public List&lt;PaymentMethod&gt; payment_methods { get; set; } } </code></pre> <p>I enter the order information and also the items for that specific order. I tried a couple of ways to add content dynamically and finally went with <a href="http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/" rel="noreferrer">Steven Sanderson's way</a>.</p> <p>In my view, I have the regular Order information and then the items, where my model looks something like this:</p> <pre><code>@model trackmeMvc.Models.Model.Order @{ ViewBag.Title = "Create"; Html.EnableClientValidation(); Html.EnableUnobtrusiveJavaScript(); } &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type="text/javascript"&gt;&lt;/script&gt; @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.") &lt;div class="editor-label"&gt; @Html.LabelFor(m =&gt; m.date)&lt;req&gt;*&lt;/req&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(m =&gt; m.date, new { @id = "order_date" })&lt;br /&gt; @Html.ValidationMessageFor(m =&gt; m.date) &lt;/div&gt; </code></pre> <p>...</p> <pre><code>&lt;script type="text/javascript"&gt; $(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; }); }); &lt;/script&gt; </code></pre> <p>Those are some of the things I tried, and nothing works. </p> <p>I got the <code>parseDynamicContent</code> from <a href="http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/" rel="noreferrer">Applying unobtrusive jquery validation to dynamic content in ASP.Net MVC</a>. I tried it in every scenario I could think of, but still no luck.</p> <p>I also tried the regular parse, removing validation from the form then applying it again, but still the newly added elements are not validated:</p> <pre><code>&lt;div id="editorRows"&gt; @foreach (var item in Model.items) { @Html.Partial("_NewItem", item) } &lt;/div&gt; </code></pre> <p>... and my partial view would look something like this:</p> <pre><code>@model trackmeMvc.Models.Model.Item @{ Layout = ""; Html.EnableClientValidation(true); if (this.ViewContext.FormContext == null) { this.ViewContext.FormContext = new FormContext(); } } &lt;div class="editRow"&gt; @using (Html.BeginCollectionItem("order_items")) { @Html.DropDownListFor(m =&gt; m.item_id, @items, "None", new { @style = "width:205px;", @id = "ddlItems", @class="ddlItem", @name="ddlItemList" }) @Html.ValidationMessageFor(m =&gt; m.item_id) ... } &lt;/div&gt; </code></pre> <p>So what's happening is, I have one empty item sent from the controller to the view by default, to show one empty row. That item is validated, but whatever comes after when I click add item, another row appears, from that partial, but I can't get it to validate. I tried to put the validation in the partial view, (before the document ready in the main form), and everything I read I applied, and it always ends up the same: validating the first row, and not the others. I tried the validation of Steven Sanderson done for that purpose - still no luck - even the validation for partials, found <a href="http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/" rel="noreferrer">at this link</a> and the page that follows which is specific to partial validation...</p> <p>What should I do to get this validation working?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload