title: Complex object and model binder ASP.NET MVC
body: <p>I have a model object structure with a <code>Foo</code> class that contains a <code>Bar</code> with a string value.</p>
  
  <pre><code>public class Foo
  {
      public Bar Bar;
  }
  
  public class Bar
  {
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>And a view model that uses that structure like this</p>
  
  <pre><code>public class HomeModel
  {
      public Foo Foo;
  }
  </code></pre>
  
  <p>I then have a form in view that in Razor looks something like this.</p>
  
  <pre><code>&lt;body&gt;
      &lt;div&gt;
          @using (Html.BeginForm("Save", "Home", FormMethod.Post))
          {
              &lt;fieldset&gt;
                  @Html.TextBoxFor(m =&gt; m.Foo.Bar.Value)
                  &lt;input type="submit" value="Send"/&gt;
              &lt;/fieldset&gt;
          }
  
      &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>In html that becomes.</p>
  
  <pre><code>&lt;form action="/Home/Save" method="post"&gt;
      &lt;fieldset&gt;
          &lt;input id="Foo_Bar_Value" name="Foo.Bar.Value" type="text" value="Test"&gt;
          &lt;input type="submit" value="Send"&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Finally the controller to handle the post loos like this</p>
  
  <pre><code>[HttpPost]
  public ActionResult Save(Foo foo)
  {
      // Magic happends here
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>My problem is that <code>Bar</code> in <code>Foo</code> is <code>null</code> once it hits the <code>Save</code> controller action (<code>Foo</code> is created but with an <code>null</code> <code>Bar</code> field).</p>
  
  <p>I thought the model binder in MVC would be able to create the <code>Foo</code> and the <code>Bar</code> object and set the <code>Value</code> property as long as it looks like the above. What am I missing?</p>
  
  <p>I also know my view model is a bit over complicated and could be simpler but I for what I'm trying to do I'd really help me if I could use the deeper object structure. The examples above uses ASP.NET 5.</p>