title: EditorFor Inheritance Model MVC
body: <p>My Post call does not return the correct Model type. It always use the baseObject instead of the correct derived object that I passed in from the Get</p>
  
  <p>RestaurantViewModel.cs</p>
  
  <pre><code>public class RestaurantViewModel{
     public Food BaseFoodObject{get;set;}
  }
  </code></pre>
  
  <p>Food.cs</p>
  
  <pre><code>public class Food{
    public string Price{get;set;)
  }
  </code></pre>
  
  <p>Bread.cs -- Inherit from Food</p>
  
  <pre><code>public class Bread:Food{
      public int Unit{get;set;} 
  }
  </code></pre>
  
  <p>Milk.cs -- Inherit from Food</p>
  
  <pre><code>public class Milk:Food{
     public string Brand{get;set}
  }
  </code></pre>
  
  <p>Editor For Template for Bread. Display the unit and allow user to edit</p>
  
  <p>Index.html</p>
  
  <pre><code>@Model RestaurantViewModel
  
  @using(Html.BeginForm("SaveFood", "Food"))
  {
    @Html.EditorFor(m=&gt;m.BaseFoodObject)
   &lt;input type="submit" value="Process"/&gt;
  }
  </code></pre>
  
  <p>Bread.cshtml</p>
  
  <pre><code>@Model Bread
  &lt;div&gt; 
     @Html.TextboxFor(bread=&gt;bread.Unit)
  &lt;/div&gt;
  </code></pre>
  
  <p>FoodController.cs</p>
  
  <pre><code>public ActionResult Index(){
    Bread bread = new Bread(){
       Price = "$10",
       Unit = 1
    }
    RestaurantViewModel viewModel = new RestaurantViewModel(){
       BaseFoodObject = bread
    }
    return View(viewModel);
  }
  
  public ActionResult Post(RestaurantViewModel viewModelPost)
  {
  // When I inspect the viewModelPost, there is no attribute for unit
  }
  </code></pre>
  
  <p>Final Result: 
  1. The display looks correct. EditorFor is smart enough to pick the correct editor template and display the value correctly
  2. The Save <strong>does not</strong> work. The <strong>Unit</strong> attribute of <strong>Bread Object</strong> does not get passed in with the <strong>RestaurantViewModel</strong>. The reason for that is the RestaurantViewModel used the <strong>Food</strong> object instead of <strong>Bread</strong></p>
  
  <p>I hope there is away to modify the EditorFor and tell it to use the <strong>Model</strong> in the <strong>View</strong> or the Object Type that I passed in when I display it.</p>
  
  <p>Thanks</p>
  
  <p>Update 1: I solved this problem by using the custom binder and using a factory to decide which object I really want. This helps construct the correct Model which I want</p>