Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 bind model to ICollection or List in partial
    text
    copied!<p>Given a Model</p> <pre><code>public class Task { public int TaskId { get; set; } public string Title { get; set; } public ICollection&lt;SomeData&gt; Information { get; set; } } </code></pre> <p>where</p> <pre><code>public class SomeData { public int SomeDataId { get; set; } public string Description { get; set; } } </code></pre> <p>I have a view</p> <pre><code>@model myProject.Models.Task &lt;div&gt; @Html.LabelFor(model =&gt; model.Title) &lt;/div&gt; &lt;table&gt; @Html.Partial("_InformationEdit", Model.Information.ToList(), new ViewDataDictionary(Html.ViewDataContainer.ViewData) { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Information" }}) &lt;/table&gt; </code></pre> <p>and my partial is</p> <pre><code>@model IList&lt;myProject.Models.SomeData&gt; @for (int i = 0; i &lt; Model.Count(); i++) { &lt;tr&gt; &lt;td&gt; @Html.EditorFor(modelItem =&gt; Model[i].Description) &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p><strong>However</strong></p> <p>My Html fields are being rendered like</p> <pre><code>&lt;input class="text-box single-line" id="Information__0__Description" name="Information.[0].Description" type="text"&gt; </code></pre> <p>Where the names should be <code>Information[0].Description</code>. It's got an additional dot in there, so is not being bound back to the model correctly when posted. How can I fix this?</p> <p>As per <strong><a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">Model binding to a list</a></strong> I can see what my Id's are supposed to be, but I just can't figure out the correct syntax.</p> <p>Also, is there a more elegant way to achieve this with an <code>IEnumerable</code> using a <code>@foreach</code> ?</p> <p><em>Related:</em> </p> <p><a href="https://stackoverflow.com/questions/12974957/asp-net-mvc4-bind-a-create-view-to-a-model-that-contains-list">ASP.Net MVC4 bind a &quot;create view&quot; to a model that contains List</a></p> <p><a href="https://stackoverflow.com/questions/653514/asp-net-mvc-model-binding-an-ilist-parameter">ASP.NET MVC model binding an IList&lt;&gt; parameter</a></p>
 

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