title: Null reference with @Html.Hidden in MVC4 Razor
body: <p>I have two views, Create and Edit. Both are having a hidden field called <code>ModelType</code> to use it in my model binder to bind all the subclasses also.</p>
  
  <p>This hidden field is working fine in Edit view, but not in Create view. I am getting a <code>null reference exception</code> at line:</p>
  
  <pre><code>@Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName) 
  </code></pre>
  
  <p>in Create view.</p>
  
  <p>What is wrong here?</p>
  
  <p>Edit.cshtml</p>
  
  <pre><code>@using PartyBiz.Models.Objects
  @model Organization
  
  @using (Html.BeginForm("Edit", "Organization", FormMethod.Post))
  {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;Edit Organization&lt;/legend&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.C) 
          @Html.TextBoxFor(model =&gt; model.C, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.C) 
      &lt;/div&gt; &lt;br /&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.N)
          @Html.TextBoxFor(model =&gt; model.N, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.N)
      &lt;/div&gt; &lt;br /&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.D)
          @Html.TextBoxFor(model =&gt; model.D, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.D)
      &lt;/div&gt; 
      &lt;br /&gt;
          @Html.HiddenFor(model=&gt; model.PID)
          @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)
          &lt;input type="submit" value="Edit" /&gt;
  
  &lt;/fieldset&gt;
  }  
  </code></pre>
  
  <p>Create.cshtml</p>
  
  <pre><code>@using PartyBiz.Models.Objects
  @model Organization
  
  @using (Html.BeginForm("Create", "Organization", FormMethod.Post))
  {
  
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;Create a New Organization&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.C) 
          @Html.TextBoxFor(model =&gt; model.C, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.C) 
      &lt;/div&gt; &lt;br /&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.N)
          @Html.TextBoxFor(model =&gt; model.N, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.N)
      &lt;/div&gt; &lt;br /&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.D)
          @Html.TextBoxFor(model =&gt; model.D, new { @class = "txt"}) 
          @Html.ValidationMessageFor(model =&gt; model.D)
      &lt;/div&gt; 
      &lt;br /&gt;
          &lt;input type="submit" value="Create" /&gt;
          @Html.Hidden("ModelType" , Model.GetType().AssemblyQualifiedName)
  
  &lt;/fieldset&gt;
  }  
  </code></pre>
  
  <p>model binder</p>
  
  <pre><code>public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          if (bindingContext.ValueProvider.ContainsPrefix("ModelType"))
          {
              //get the model type
              var typeName = (string)bindingContext
                  .ValueProvider
                  .GetValue("ModelType")
                  .ConvertTo(typeof(string));
              var modelType = Type.GetType(typeName);
  
              //tell the binder to use it
              bindingContext.ModelMetadata =
                  ModelMetadataProviders
                  .Current
                  .GetMetadataForType(null, modelType);
          }
          return base.BindModel(controllerContext, bindingContext);
      }
  </code></pre>