title: ASP.Net MVC 2: Using a viewmodel destroys my model binding
body: <p>I guess this is a story of how frameworks cheerfully do 95% of what you need, but then frown disapprovingly at that final five percent; inform you that if you want to participate in nonstandard malarky it's your own business, thank you very much, and it'll be here if you decide you want to return to doing things it's good at. Generally speaking it's inevitable that this final five percent will contain some version of a must-have feature. </p>
  
  <p>I have a strongly-typed view which updates a data object. I've used idiomatic MVC2 helpers, eg <code>Html.TextBoxFor(model = &gt; model.Name)</code>. I've used Editor templates for nested objects. (My backend is a collection of Mongo documents, so I need to represent complex types).</p>
  
  <p>Then I need a dropdown. It turns out dropdowns are a bit finicky; no problem, I'll make a viewmodel instead of passing in the <code>item</code> directly:</p>
  
  <pre><code>class itemViewModel
  {
      ...
      public Item item { get; set; }
      public IEnumerable&lt;SelectListItem&gt; dropdown { get; set; }
  }
  
  public ActionResult()
  {
      return View("Update", new itemViewModel(item, dropdown))
  }
  </code></pre>
  
  <p>... that works fine, the dropdown populates. But! my view requires updating:</p>
  
  <pre><code>Html.TextBoxFor(model =&gt; model.Name) -&gt;
  Html.TextBoxFor(model =&gt; model.item.Name)
  </code></pre>
  
  <p>Great, problem solved. Oops, now my model binding doesn't work. I debug and look at the <code>Request.Form</code> values: Oh. <code>item.Name</code> instead of <code>Name</code>. Makes sense. I tell my Update view to expect an <code>itemViewModel</code> instead, and the binding works. </p>
  
  <p>Oh wait, no it doesn't. Because I have nested objects that use editors. They are strongly typed and they don't know that the model they're receiving is actually a property of the viewmodel. So they're still spitting out <code>Address.City</code> instead of <code>item.Address.City</code>, and the binding fails.</p>
  
  <p>I can think of several workarounds:</p>
  
  <ol>
  <li>Write specialized custom model binder</li>
  <li>Put the whole damn form into its own typed editor, so it gets the <code>item</code> model without knowing it's a property</li>
  <li>Kill the viewmodel and hack the dropdown using the <code>ViewData</code> dictionary</li>
  <li>Quit using the <code>HtmlHelpers</code> and hand-write the whole form</li>
  <li>Write my own <code>HtmlHelper</code> extensions that will take a lamba <em>and</em> a model object as parameters.</li>
  <li>Put every label/field grouping into an individual editor template.</li>
  </ol>
  
  <p>All of these feel like either overkill or sloppiness. Viewmodels seem to be a clean, helpful approach. Does using them mean that I have to be sloppy in other areas, or reproduce minor variations on sizeable chunks of the framework? I taught myself C# over the last three months (a graphic designer trying to figure out what the hell static typing is with no CS background was probably pretty funny to watch). I work in isolation; there's no one to learn best practices from. I feel like if I don't learn some of them, I'll end up with an unmaintainable dung heap. So, your opinions are appreciated.  </p>