title: MVC 5 Generic List Binding to Partial View
body: <p>I'm having an issue with the bindings of my model to a partial view and feel that there must be a way to do what I want.  I wonder if my design is flawed and a small refactoring might be necessary.  </p>
  
  <p>A very simplified (and abstract) version of my models would be like so:</p>
  
  <pre><code>Public Class BaseClass
  {
      Public string Name { get; set; }
      Public List&lt;SomeClass&gt; Things { get; set; }
  }
  
  Public Class DerivedClass : BaseClass
  {
      Public List&lt;LineItem&gt; Items { get; set; }
  }
  
  Public Class Library
  {
      Public List&lt;LineItem&gt; Items { get; set; }
  }
  
  Public Class LineItem
  {
      Public string Name { get; set; }
      Public string Value { get; set; }
  }
  </code></pre>
  
  <p>I have Editor Templates for the BaseClass, SomeClass, and LineItem.  These are shown in the view for DerivedClass and work as intended by submitting changes to the controller.  The LineItem template is wrapped in a LineItemList partial view because I intend to use it for a view for Library and don't want to repeat all of that layout and javascript.  The LineItemList partial view is included on the DerivedClass view by Html.PartialView since there doesn't seem to be a way to create an Editor Template for the List type.  So my views look like this:</p>
  
  <ul>
  <li>DerivedClassView
  
  <ul>
  <li>BaseClassPartialView
  
  <ul>
  <li>SomeClassPartialView</li>
  </ul></li>
  <li>LineItemListPartialView
  
  <ul>
  <li>LineItemParialView </li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>When I submit my form, the controller gets all of the data for the BaseClass and SomeClass list but none for the LineItem list.  The difference of course being that one is rendered using Html.EditorFor and the other Html.PartialView.  </p>
  
  <p>Refactoring the classes will be tough as they have to be backwards compatible with an old XML format for serialization, but I'm sure I can work some magic if necessary.</p>
  
  <p>As Chris Pratt mentioned, I forgot to include my controller methods:</p>
  
  <pre><code>Public ActionResult DerivedClassEditor()
  {
      Return View(New DerivedClass());
  }
  
  [HttpPost]
  Public ActionResult DerivedClassEditor(DerivedClass dc)
  {
      // Do Stuff
  }
  </code></pre>
  
  <p>I just noticed in the rendered Html, the SomeClass controls are named <code>SomeClass.[0].Name</code> while those of the LineItem are <code>[0].Name</code>.  I have a feeling that might be a symptom of the issue.</p>
  
  <p>And my views look similar to this:</p>
  
  <p><strong>DerivedClassEditor</strong></p>
  
  <pre><code>@model DerivedClass
  
  @using (Html.BeginForm())
  {
      @Html.EditorFor(model =&gt; model)
  
      @Html.Partial("LineItemListPartialView")
  
      &lt;input type="submit" /&gt;
  }
  </code></pre>
  
  <p><strong>LineItemListPartialView</strong></p>
  
  <pre><code>@model List&lt;LineItem&gt;
  
  &lt;div name="Items"&gt;
      @Html.EditorFor(model =&gt; model)
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>LineItemPartialView</strong></p>
  
  <pre><code>@model LineItem
  
  &lt;div name="LineItem"&gt;
      @Html.LabelFor(model =&gt; model.Name)
      @Html.TextEditorFor(model =&gt; model.Name)
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <p>A link to my view:  <a href="https://github.com/melance/TheRandomizer/blob/Initial/TheRandomizer.WebApp/Views/UserContent/AssignmentEditor.cshtml" rel="nofollow noreferrer">https://github.com/melance/TheRandomizer/blob/Initial/TheRandomizer.WebApp/Views/UserContent/AssignmentEditor.cshtml</a></p>
  
  <p>I've narrowed down the issue to the fact that when I load one of the lists using @Html.EditorFor it names the inputs <code>Collection[index].Property</code> yet when I add one dynamically using the same call it simply names the input <code>Property</code>.  Is there an easy and reusable way to have the addition of new items have the same naming structure?</p>