title: MVC binding to model with list property ignores other properties
body: <p>I have a basic ViewModel with a property that is a List of complex types. When binding, I seem to be stuck with getting either the list of values, OR the other model properties depending on the posted values (i.e. the view arrangement).</p>
  
  <p>The view model:</p>
  
  <pre><code>public class MyViewModel
  {
      public int Id { get; set; }
      public string Property1 { get; set; }
      public string Property2 { get; set; }
  
      public List&lt;MyDataItem&gt; Data { get; set; }
  }
  
  public class MyDataItem
  {
      public int Id { get; set; }
      public int ParentId { get; set; }
      public string Name { get; set; }
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>The controller actions:</p>
  
  <pre><code>    public ActionResult MyForm()
      {
          MyViewModel model = new MyViewModel();
  
          model.Id = 1;
          model.Data = new List&lt;MyDataItem&gt;() 
          { 
              new MyDataItem{ Id = 1, ParentId = 1, Name = "MyListItem1", Value = "SomeValue"}
          }; 
  
          return View(model);
      }
  
      [HttpPost]
      public ActionResult MyForm(MyViewModel model)
      {
          //...
  
          return View(model);
      }
  </code></pre>
  
  <p>Here is the basic view (without the list mark-up)</p>
  
  <pre><code>@using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;My View Model&lt;/legend&gt;
  
          @Html.HiddenFor(model =&gt; model.Id)
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Property1)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Property1)
              @Html.ValidationMessageFor(model =&gt; model.Property1)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Property2)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Property2)
              @Html.ValidationMessageFor(model =&gt; model.Property2)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>When posted back to the controller, I get the 2 property values and a null value for the 'Data' property as expected. </p>
  
  <p><img src="https://i.stack.imgur.com/Uspzv.gif" alt="Without List Mark-up"></p>
  
  <p>If I add the mark-up for the List as follows (based on the information in this <a href="http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx" rel="nofollow noreferrer">Scott Hanselman post</a> and <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx" rel="nofollow noreferrer">Phil Haack's post</a>):</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
  @for (int i = 0; i &lt; Model.Data.Count(); i++)
  {
      MyDataItem data = Model.Data[i];
      @Html.Hidden("model.Data[" + i + "].Id", data.Id)
      @Html.Hidden("model.Data[" + i + "].ParentId", data.ParentId)
      @Html.Hidden("model.Data[" + i + "].Name", data.Name)
      @Html.TextBox("model.Data[" + i + "].Value", data.Value)
  }
  &lt;/div&gt;
  </code></pre>
  
  <p>The 'Data' property of the model is successfully bound but the other properties are null. </p>
  
  <p><img src="https://i.stack.imgur.com/CAIKg.gif" alt="With List Mark-up"></p>
  
  <p>The form values posted are as follows:</p>
  
  <pre><code>Id=1&amp;Property1=test1&amp;Property2=test2&amp;model.Data%5B0%5D.Id=1&amp;model.Data%5B0%5D.ParentId=1&amp;model.Data%5B0%5D.Name=MyListItem1&amp;model.Data%5B0%5D.Value=SomeValue
  </code></pre>
  
  <p>Is there a way to get both sets of properties populated or am I just missing something obvious?</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>For those of you who are curious. Based on the answer from <a href="https://stackoverflow.com/users/2972/martinhn">MartinHN</a>, the original generated mark-up was:</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
      &lt;input id="model_Data_0__Id" name="model.Data[0].Id" type="hidden" value="1" /&gt;
      &lt;input id="model_Data_0__ParentId" name="model.Data[0].ParentId" type="hidden" value="1" /&gt;
      &lt;input id="model_Data_0__Name" name="model.Data[0].Name" type="hidden" value="MyListItem1" /&gt;
      &lt;input id="model_Data_0__Value" name="model.Data[0].Value" type="text" value="SomeValue" /&gt;        
  &lt;/div&gt;
  </code></pre>
  
  <p>The new generated mark-up is:</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
      &lt;input id="Data_0__Id" data-val="true" name="Data[0].Id" type="hidden" value="1" data-val-number="The field Id must be a number." data-val-required="The Id field is required." /&gt;
      &lt;input id="Data_0__ParentId" name="Data[0].ParentId" type="hidden" value="1"  data-val="true" data-val-number="The field ParentId must be a number." data-val-required="The ParentId field is required." /&gt;
      &lt;input id="Data_0__Name" name="Data[0].Name" type="hidden" value="MyListItem1" /&gt;
      &lt;input id="Data_0__Value" name="Data[0].Value" type="text" value="SomeValue" /&gt;        
  &lt;/div&gt;
  </code></pre>
  
  <p>Which results in the following posted values:</p>
  
  <pre><code>Id=1&amp;Property1=test1&amp;Property2=test2&amp;Data%5B0%5D.Id=1&amp;Data%5B0%5D.ParentId=1&amp;Data%5B0%5D.Name=MyListItem1&amp;Data%5B0%5D.Value=SomeValue
  </code></pre>
  
  <p>Notice there's no 'model.' in the name and posted values...</p>