title: ViewModel inside of ViewModel - How to get it to post?
body: <p>I have a viewmodel inside of another viewmodel for seperation of concerns. I created an editor template for it and set up the default values in the controller at runtime. Unfortunately when the parent view model posts to the controller, it does not save the values of the child view models' items. Here is the code:</p>
  
  <p>Note: Some code names were changed, so if there's any incongruities please point it out in the comment. I've gone over it about 4x and found them all I think. </p>
  
  <pre><code>public class ParentViewModel {
      public ChildViewModel {get;set;}
  }
  public class ChildViewModel {
      public List&lt;Item&gt; Items {get;set;}
  }
  public class Item {
      public int Id {get;set;
      public string Name {get;set;}
  }
  </code></pre>
  
  <p>I've created an EditorTemplate that binds properly on the view</p>
  
  <pre><code>@model MyProject.ViewModels.ChildViewModel
  
  @foreach (var item in Model.Items)
  {
      &lt;div class="Item" @String.Format("id=Item{0}", @item.Id) &gt;
          Item #@Html.DisplayFor(models =&gt; item.Id): 
          @Html.LabelFor(model =&gt; item.Name)
          @Html.EditorFor(model =&gt; item.Name)
      &lt;/div&gt;   
  }
  </code></pre>
  
  <p>However, when I submit the form that the ParentViewModel is bound to, the ChildViewModel's items are null!</p>
  
  <p>Controller.cs</p>
  
  <pre><code>public class ControllerController{
      public ActionResult Form {
          return View(new ParentViewModel {
              ChildViewModel = new ChildViewModel {
                  Items = new List&lt;Item&gt;(Enumerable.Range(1,20).Select(i =&gt; new Item { Id=i })
              }
          });
      }
      [HttpPost]
      [ActionName("Form")]
      public class ActionResult FormSubmitted(ParentViewModel parentViewModel) {
          //parentViewModel.ChildViewModel.Items is null!
          _fieldThatIsRepresentingMyDataService.Save(parentViewModel);
      }
  }
  </code></pre>
  
  <p>ViewView.cshtml</p>
  
  <pre><code> &lt;div class="editor-label"&gt;
       @Html.LabelFor(model =&gt; model.ChildViewModel)
  &lt;/div&gt;
  &lt;div id="ItemList" class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.ChildViewModel)
  &lt;/div&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>