Note that there are some explanatory texts on larger screens.

plurals
  1. PODefine the name of the input parameter
    primarykey
    data
    text
    <p>I'm submiting some data (<code>Collection&lt;Cars&gt;</code>) with JQuery by AJAX but now I don't know how to define the object in my Action input parameter. I already tried to define as <strong>FormCollection</strong> and I received all data. However what I really want is to define as <strong><code>ICollection&lt;Car&gt;</code></strong> but when I do this my object <strong>cames to null</strong>. How can I do this? Or how can I <strong>parse a FormCollection</strong> to a <code>ICollection&lt;Car&gt;</code>?</p> <p><strong>My controller:</strong></p> <pre><code>public ActionResult UpdateTable(ICollection&lt;Car&gt; collection) { //some code } </code></pre> <p><strong>My js:</strong></p> <pre><code>function updateTable(tableId) { if (tableId == "myTable") { event.preventDefault(); if ($("#" + tableId + " tbody tr").length % 2 == 0) var myTableData = $("#" + tableId).find("select, input").serializeObject(); $.post("../../Car/UpdateTable/", myTableData , function(partial) { $("#myOtherTable tbody").append(partial); }); } } $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; </code></pre> <p><strong>Car.cs:</strong></p> <pre><code>public int Id { get; set; } public string Name { get; set; } public ICollection&lt;Owner&gt; Owners { get; set; } public bool IsSold { get; set; } </code></pre> <p><strong>Results:</strong></p> <p>What comes to action controller when input parameter is <code>ICollection&lt;Car&gt;</code>: <strong>null</strong></p> <p>What comes to action controller when input parameter is <code>FormCollection</code> (in AllKeys):</p> <pre><code>Car[0].Id Car[0].Name Car[0].Owners IsSold </code></pre> <p><strong>My view (row table):</strong></p> <pre><code> &lt;tr&gt; &lt;td&gt; @Html.TextBoxFor(model =&gt; model.Id, new { Name = modelFieldInitialName + "Id", id = modelFieldInitialId + "Id" }) @Html.ValidationMessageFor(model =&gt; model.Id) &lt;/td&gt; &lt;td&gt; @Html.TextBoxFor(model =&gt; model.Name, new { Name = modelFieldInitialName + "Name", id = modelFieldInitialId + "Name" }) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/td&gt; &lt;td&gt; @Html.DropDownListFor(model =&gt; model.Owners, new SelectList(TempData["OwnersList"] as IEnumerable, "Id", "Description"), "", new { Name = modelFieldInitialName + "Owners", id = modelFieldInitialId + "Owners" }) @Html.ValidationMessageFor(model =&gt; model.Owners) &lt;/td&gt; &lt;td&gt; @Html.CheckBoxFor(model =&gt; model.IsSold, new { type = "checkbox", Name = modelFieldInitialName + "IsSold", id = modelFieldInitialId + "IsSold" }) @Html.ValidationMessageFor(model =&gt; model.IsSold) &lt;/td&gt; &lt;/tr&gt; </code></pre>
    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.
    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