title: KnownType attribute on a property during model binding in MVC?
body: <p>Lets say I have the class Food and a class BirdFood that derives from Food:</p>
  
  <pre><code>public class Food
  {
     ...
  }
  
  public class BirdFood : Food
  {
      public int SeedCount { get; set; }
  }
  </code></pre>
  
  <p>And lets say I have a class Creature and a class Bird that derives from Creature</p>
  
  <pre><code>public class Creature
  {
      public Food Food { get; set; }
  }
  
  public class Bird : Creature
  {
     ...
  }
  </code></pre>
  
  <p>And finally, lets say I have a view model for Creature:</p>
  
  <pre><code>public class ViewModel
  {
      public Creature Creature { get; set; }
  }
  </code></pre>
  
  <p>When I create an instance of Bird, I also create an instance of BirdFood and assign it to the Food property and pass the view model to the View. Simple enough.</p>
  
  <pre><code>    public ActionResult Index()
      {
          ViewModel viewModel = new ViewModel();
  
          viewModel.Creature = new Bird();
  
          viewModel.Creature.Food = new BirdFood() { SeedCount = 100 } ;
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>And the view:</p>
  
  <pre><code>&lt;% using(Html.BeginForm("Save", "Home", FormMethod.Post))
     { %&gt;
  
     &lt;%: Html.Hidden("Creature.Food.SeedCount", (Model.Creature.Food as MvcApplication6.Controllers.BirdFood).SeedCount)  %&gt;
  
     &lt;input type="submit" /&gt;
  
  &lt;% } %&gt;
  </code></pre>
  
  <p>Okay, here is the problem. When the view model is received in the Action, the Creature property is of the base class Creature instead of the derived type Bird with the SeedCount property of the derived type BirdFood with the post parameters (from firebug) that looks like <code>Creature.Food.SeedCount    100</code></p>
  
  <p><img src="https://i.stack.imgur.com/ojtSH.png" alt="enter image description here"></p>
  
  <p>Okay, my questions is, how do I preserve the derived types when model binded? I'm suspecting it has to do with a custom Model Binder but I'm clueless when it comes to that. Any ideas?</p>