Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So your issue is that the model binding isn't happening correctly.</p> <p>Say you have these two models:</p> <pre><code>public class ParentModel { public string Name {get;set;} public ChildModel Child {get;set;} } public class ChildModel { public string ChildName {get;set;} public int SomeNumber {get;set;} } </code></pre> <p>Your generated HTML (for your model binding to happen correctly) needs to look like this:</p> <pre><code>&lt;input name="Name" value="(Not relevant to this example)"/&gt; &lt;input name="Child.ChildName" value="(Not relevant to this example)" /&gt; </code></pre> <p>Note how the name field is structured - this is how MVC determines what input values map to which properties in your view model. With the nested property, the property name has to go in front of it.</p> <p>With collections, it gets more complicated. The model binder needs to know which values go with which instance of a property.</p> <p>For example, if we assume the ChildModel is of type IEnumerable in the previous example, your HTML might look something like this in order to model bind correctly:</p> <pre><code>&lt;input name="Name" value="(Not relevant to this example)"/&gt; &lt;input name="Child[0].ChildName" value="(Not relevant to this example)" /&gt; &lt;input name="Child[0].SomeNumber" value="(Not relevant to this example)"/&gt; &lt;input name="Child[1].ChildName" value="(Not relevant to this example)" /&gt; &lt;input name="Child[1].SomeNumber" value="(Not relevant to this example)"/&gt; </code></pre> <p>Take a look at these for how to fix it:</p> <p><a href="http://seesharpdeveloper.blogspot.com/2012/05/mvc-model-binding-to-list-of-complex.html">http://seesharpdeveloper.blogspot.com/2012/05/mvc-model-binding-to-list-of-complex.html</a> <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx">http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx</a></p> <p><strong>Edit</strong> - It's also important to note that when the Html Helpers generate the name value, it's based on the lambda value that is passed in. So</p> <pre><code>@Html.CheckBoxFor(m =&gt; state.IsChecked) </code></pre> <p>will generate the following name</p> <pre><code>name="state.IsChecked" </code></pre> <p>Since you're within a foreach, you're getting the wrong value for the name.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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