Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 collection model binding with EditorFor
    primarykey
    data
    text
    <p>Regarding this <a href="https://stackoverflow.com/a/5623498/607162">post</a> and this <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">other one</a>.</p> <p>Suppose I have the following:</p> <pre><code>public class Foo { public string Value1 { get; set; } public string Value2 { get; set; } } public class BarViewModel { public string Baz { get; set; } public IList&lt;Foo&gt; Foos { get; set; } } </code></pre> <p>And I have a view that receive a <code>BarViewModel</code>:</p> <pre><code>@model BarViewModel @Html.EditorFor(model =&gt; model.Baz) &lt;table&gt; @for(int i = 0 ; i &lt; Model.Foos.Count ; i ++) { string name1 = "Foos[" + i.ToString() + "].Value1"; string name2 = "Foos[" + i.ToString() + "].Value2"; &lt;tr&gt; &lt;td&gt; &lt;input type="text" name="@name1" value="@Model.Foos[i].Value1" /&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" name="@name2" value="@Model.Foos[i].Value2" /&gt; &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre> <p>And in my controller I have a POST method that recive the <code>BarViewModel</code>.</p> <p>Given the inputs names generated for Value1 and Value2 are <code>"Foos[0].Value1"</code> and <code>"Foos[1].Value1"</code> and so on, the collection on the BarViewModel, in the POST method, is automatically filled by the ModelBinder. Awesome.</p> <p>The problem is, if I do it this way in my view :</p> <pre><code> @for(int i = 0 ; i &lt; Model.Foos.Count ; i ++) { &lt;tr&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.Foos[i].Value1); &lt;/td&gt; &lt;td&gt; @Html.EditorFor(model =&gt; model.Foos[i].Value2); &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>Then the names generated for the input are like <code>"Foos__0__Value1"</code>, <em>and that break the model binding</em>. The <code>Foos</code> property of my BarViewModel, in my POST method, is now <code>null</code></p> <p>I am missing something?</p> <h2>Edit</h2> <hr> <p>If I use <code>EditorFor</code> on the collection itself:</p> <pre><code>@EditorFor(model =&gt; model.Foos) </code></pre> <p>The names are generated correctly. But that force me to build a ViewModel in /Views/Share to handle the type <code>Foos</code>, that will generate the row, wich I dont really want to do...</p> <h2>Edit 2</h2> <hr> <p>I will clarify my question here, I understand that it's a bit vague.</p> <p>If I do :</p> <pre><code>@EditorFor(model =&gt; model.Foos) </code></pre> <p>The names of the inputs will have the form <code>"Foos[0].Value1"</code> and the model binding works just fine on posts.</p> <p>But if I do :</p> <pre><code>@for(int i = 0 ; i &lt; Model.Foos.Count ; i ++) { @EditorFor(model =&gt; Model.Foos[0].Value1) } </code></pre> <p>The names takes the form <code>"Foos__0__Value1"</code> and the model binding <em>does not works</em>. On my post method, model.Foos will be null.</p> <p>Is there a reason why the second syntax breaks the model binding?</p>
    singulars
    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