title: Why does TryUpdateModel return false even though properties are set and valid?
body: <p>I have web application based on ASP.Net MVC3. I have a need for a "Create" view which will not know the model type until the user picks a sub-type to create from a drop-down. To attempt to solve this problem, I have created an editor template under Shared/EditorTemplates for each derived model type. This allows me to create a single "Create.cs" which is strongly-typed to a view model. The view model only has two members, an enum and a complex type. The idea is that the view will initially show only a drop-down (editor for the enum member) then when the user initially submits the specified "model type" (drop-down selected value), the POST action can check the "model type" specified and instantiate the correct derived model type for the view model's single complex member who's type is the base type for all possible "model types". </p>
  
  <p>The abstract + derived type model objects...</p>
  
  <pre><code>public abstract class MyModelBase
  {
      public MyModelType_e ModelType {get; set; }
      [Required]
      public string Name { get; set; }
  }
  
  public class DerivedType1 : MyModelBase
  { ... }
  
  public class DerivedType2 : MyModelBase
  { ... }
  
  public class DerivedType3 : MyModelBase
  { ... }
  </code></pre>
  
  <p>I have a complex view model as follows...</p>
  
  <pre><code>public enum MyModelType_e
  {
      DerivedType1 = 0,
      DerivedType2 = 1,
      DerivedType3 = 2
  }
  
  public class MyModelCreate
  {
      public MyModelType_e ModelTypeForSelectList { get; set; }
      public MyModelBase ModelBase { get; set; }
  }
  </code></pre>
  
  <p>My GET controller action instantiates the above view model for the view i.e., only a drop-down list displayed with items based on the MyModelType_e enum + the value of the model's "ModelBase" property is initially null. So the GET action method looks like this...</p>
  
  <pre><code>[HttpGet]
  public ActionResult Create()
  {
      return View(new MyModelCreate());
  }
  </code></pre>
  
  <p>Note the comment in caps bellow regarding the crux of my issue which is that TryUpdateModel fails (see below) even though it sets the properties of the ModelBase (derived-type) member to the corresponding form values as expected...</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(MyModelCreate model)
  {
      if (model.ModelBase == null || 
          (int)model.ModelTypeForSelectList != model.ModelBase.ModelType)
      {
          switch (model.ModelType)
          {
              case MyModelType_e.DerivedType1:
                  model.ModelBase = new DerivedType1();
                  break;
              case MyModelType_e.DerivedType2:
                  model.ModelBase = new DerivedType2();
                  break;
              case MyModelType_e.DerivedType3:
                  model.ModelBase = new DerivedType3();
                  break;
          }
          return View(model);
      }
  
      if (!TryUpdateModel(model.ModelBase))
          return View(model); // &lt;&lt;&lt; THIS HAPPENS EVEN THOUGH ModelBase APPEARS TO BE UPDATED PROPERLY... 
      // For instance, I can see right here with intellisense that model.ModelBase.Name
      // is NOT null or empty but rather is truly updated with the correct form value(s)...
  
      // TODO: Insert the record, etc... (currently we never get here...)
  }
  </code></pre>
  
  <p>So the above section is where the problem stems from but here is my view to help understand...</p>
  
  <pre><code> @model MyNamespace.MyModelCreate
  
   &lt;h2&gt;Create&lt;/h2&gt;
  
   ...
  
   @using (Html.BeginForm())
   {
       @Html.ValidationSummary(false)
      &lt;fieldset&gt;
      &lt;legend&gt;Input&lt;/legend&gt;
      &lt;div class="editor-label"&gt;
      @Html.Label("Select Model Type")
      &lt;/div&gt;
      &lt;div&gt;
      @Html.EnumDropDownListFor(model =&gt; model.ModelType)
      @Html.ValidationMessageFor(model =&gt; model.ModelType)
      &lt;/div&gt;
  
      @* 
      Conditionally show the correct editor template... 
      There is one existing under ../Shared/EditorTemplates for each
      derived type (DerivedType1, DerivedType2, DerivedType3, etc...)
      This much is working in the sense that the correct editor fields
      are displayed based on what the user selects in the above drop-down.
      *@
      @if (Model.InputModel != null)
      {  
          @Html.EditorFor(model =&gt; model.ModelBase);
      }
  
      &lt;p&gt;
          &lt;input type="submit" value="Continue" /&gt;
      &lt;/p&gt;              
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>So once with the initial POST (model type is selected), my POST action method falls through to the TryUpdateModel line by design but for some reason the validation fails. The part I really don't understand is that the validation summary reports "Name is required" even though I can clearly watch TryUpdateModel set the Name propery properly on the ModelBase member of the view model.</p>
  
  <p>I'd greatly appreciate any help or guidance here... I am fairly new to ASP.NET MVC and I am open to doing things differently since I understand there are probably other ways I could design my requests/actions + views to accomplish this "multi-step" problem but I really am just going for the simplest possible thing which I why I like the EditorTemplate approach for handling the derived model types, etc.</p>
  
  <p>Thanks in advance...</p>