title: Using dynamic objects with ASP.NET MVC model binding
body: <p>In an ASP.NET MVC3 application, if I wanted to model bind my form post data to an <code>ExpandoObject</code> (or my own object derived from <code>DynamicObject</code> where I implement my own <code>Try...</code> members) would I need to write my own custom model binder?</p>
  
  <p>If I do:</p>
  
  <pre><code>public ActionResult Save(ExpandoObject form)
  {
      ....
  }
  </code></pre>
  
  <p>The value of <code>form</code> is <code>null</code>.</p>
  
  <p>Or if I have:</p>
  
  <pre><code>public class MyDynamicThing : DynamicObject
  {
      public int Id { get; set; }
  
      public override bool TrySetMember(SetMemberBinder binder, object value)
      {
          // Set breakpoint here but doesn't get hit when model binding
          return base.TrySetMember(binder, value);
      }
  }
  </code></pre>
  
  <p>...and in my controller:</p>
  
  <pre><code>public ActionResult Save(MyDynamicThing form)
  {
      ....
  }
  </code></pre>
  
  <p>In the above example <code>Id</code> is set to the value from the form. However if I set a breakpoint in <code>TrySetMember</code> this doesn't get hit.</p>
  
  <p>Are there any magical incantations I can invoke to coerce the built-in model binder to work with <code>ExpandoObjects</code> or my own classes derived from <code>DynamicObject</code>?</p>
  
  <p>I could resort to picking up the raw form post collection but I have to serialise this data to JSON which would mean an extra and untidy step to harvest those values.</p>