title: getting the values from a nested complex object that is passed to a partial view
body: <p>I have a ViewModel that has a complex object as one of its members.  The complex object has 4 properties (all strings).  I'm trying to create a re-usable partial view where I can pass in the complex object and have it generate the html with html helpers for its properties.  That's all working great.  However, when I submit the form, the model binder isn't mapping the values back to the ViewModel's member so I don't get anything back on the server side.  How can I read the values a user types into the html helpers for the complex object.</p>
  
  <p>ViewModel</p>
  
  <pre><code>public class MyViewModel
  {
       public string SomeProperty { get; set; }
       public MyComplexModel ComplexModel { get; set; }
  }
  </code></pre>
  
  <p>MyComplexModel</p>
  
  <pre><code>public class MyComplexModel
  {
       public int id { get; set; }
       public string Name { get; set; }
       public string Address { get; set; }
       ....
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public class MyController : Controller
  {
       public ActionResult Index()
       {
            MyViewModel model = new MyViewModel();
            model.ComplexModel = new MyComplexModel();
            model.ComplexModel.id = 15;
            return View(model);
       }
  
       [HttpPost]
       public ActionResult Index(MyViewModel model)
       {
            // model here never has my nested model populated in the partial view
            return View(model);
       }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@using(Html.BeginForm("Index", "MyController", FormMethod.Post))
  {
       ....
       @Html.Partial("MyPartialView", Model.ComplexModel)
  }
  </code></pre>
  
  <p>Partial View</p>
  
  <pre><code>@model my.path.to.namespace.MyComplexModel
  @Html.TextBoxFor(m =&gt; m.Name)
  ...
  </code></pre>
  
  <p>how can I bind this data on form submission so that the parent model contains the data entered on the web form from the partial view?</p>
  
  <p>thanks</p>
  
  <p>EDIT: I've figured out that I need to prepend "ComplexModel." to all of my control's names in the partial view (textboxes) so that it maps to the nested object, but I can't pass the ViewModel type to the partial view to get that extra layer because it needs to be generic to accept several ViewModel types.  I could just rewrite the name attribute with javascript, but that seems overly ghetto to me.  How else can I do this?</p>
  
  <p>EDIT 2: I can statically set the name attribute with new { Name="ComplexModel.Name" } so I think I'm in business unless someone has a better method?</p>