title: How to get data of EditorFor with nested viewmodels
body: <p>Here is my situation - </p>
  
  <p>I have two nested view models:</p>
  
  <ol>
  <li><code>&lt;%=Html.EditorFor(x =&gt; x.DisplayEntitiesWithRadioboxesViewModel)%&gt;&lt;br /&gt;</code></li>
  </ol>
  
  <p>Which sit within their parent (StructureViewModel), I can populate the nested ViewModels easily and pass it through to the main View:</p>
  
  <p><strong>Within the Controller - Example</strong></p>
  
  <pre><code>var moveDepartment = new StructureViewModel();
  moveDepartment.DisplayEntitiesWithRadioboxesViewModel = fullDepartmentList.Select(x =&gt; new DisplayEntityViewModel
              {
                  Id = x.Id,
                  Path = x.Path,
                  PathLevel = x.PathLevel,
                  Description = x.Description,
              });
  
  return View(moveDepartment);
  </code></pre>
  
  <p><strong>EditorTemplete - Example</strong></p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;Site.Areas.Administration.ViewModel.DisplayEntityViewModel&gt;&gt;" %&gt;
  &lt;table class="aligncenter"&gt;&lt;%
    if (Model != null)
    {
      foreach (var entity in Model)
      {%&gt;
        &lt;tr class="tRow"&gt;
          &lt;td style="text-align:left; text-indent:&lt;%=Html.Encode(entity.PathLevel)%&gt;em"&gt;
            &lt;%=Html.Encode(entity.Description)%&gt;
            &lt;%=Html.RadioButton("radiobutton",entity.Id)%&gt;
          &lt;/td&gt;              
        &lt;/tr&gt;&lt;%    
      }
    }%&gt;
  &lt;/table&gt;
  
  
  namespace Site.Areas.Administration.ViewModel
  {
      public class DisplayEntityViewModel
      {
          public int Id { get; set; }
          public string Path { get; set; }
          public string PathLevel { get; set; }
          public string Description { get; set; }   
      }
  }
  </code></pre>
  
  <p>However when I try to pull back this information the nested ViewModels are null:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Move(StructureViewModel StructureViewModel)
  </code></pre>
  
  <p>When I hover over <code>StructureViewModel</code> it only contains data set at the parent ViewModel. For example: a hidden value can been seen but DisplayEntitiesWithRadioboxesViewModel = null.</p>
  
  <p>The only way I know how to access the <code>DisplayEntitiesWithRadioboxesViewModel</code> is to use <code>FormCollection</code> and iterate throught the <code>FormCollection</code> and pull out the information I need from the nested ViewModels.</p>
  
  <p>This however just doesn't seem right, as I have found at I then have to re-populate the <code>DisplayEntitiesWithRadioboxesViewModel</code> with the values from the <code>FormCollection</code>, if for example an error has occured and the user needs to be sent back to the same View.</p>
  
  <p>I have tried searching the web/books but cannot find a solution.</p>
  
  <p>Is there a better way? </p>
  
  <p>Thanks in advance for any help.</p>
  
  <blockquote>
    <p>And why did you use an EditorFor for a
    simple dropdown, which is easily to
    use with DropDownFor</p>
  </blockquote>
  
  <p>This has now been altered to use the DropDownFor.</p>
  
  <blockquote>
    <p>what is the Key of the
    DisplayEntitiesWithRadioboxesViewModel
    value in FormCollection</p>
  </blockquote>
  
  <pre><code>{string[3]}
  [0] = "DisplayEntitiesWithRadioboxesViewModel.radiobutton"
  [1] = "Action"
  [2] = "OldParentId"
  </code></pre>
  
  <p>Clare :-)</p>