title: Receiving a NullReference Exception error during HttpPost Action when using a ViewModel
body: <p>I am trying to create what I feel is a very simple form submission using a ViewModel.  I have worked on this off and on all day and for some reason cannot understand why when my app gets to my HttpPost action my EmailViewModel is empty.  I get a "NullReference Exception Occurred" "Object reference not set to an instance of an object" error.</p>
  
  <p>Can you take a look at my code and tell me where I am being crazy?</p>
  
  <p>Here is my httpPost action:</p>
  
  <pre><code>[HttpPost]
  public ActionResult SendStudentAnEmail(EmailViewModel email)
  {
          Debug.Write(email.Subject); // First NullReferenceException
          Debug.Write(email.Body);
          Debug.Write(email.Email);
          etc. . .
  </code></pre>
  
  <p>My ViewModel:</p>
  
  <pre><code>namespace MyApp.ViewModels
  {
      public class EmailViewModel
      {
          public string Email { get; set; }
  
          public string Subject { get; set; }
  
          public string Body { get; set; }
      }
  }
  </code></pre>
  
  <p>and My View:</p>
  
  <pre><code>@model MyApp.ViewModels.EmailViewModel
  
  @{
      ViewBag.Title = "SendStudentAnEmail";
  }
  
  &lt;h2&gt;SendStudentAnEmail&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;EmailViewModel&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Email)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Email)
              @Html.ValidationMessageFor(model =&gt; model.Email)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Subject)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Subject)
              @Html.ValidationMessageFor(model =&gt; model.Subject)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Body)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Body)
              @Html.ValidationMessageFor(model =&gt; model.Body)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>Thank you.</p>
  
  <p><em><strong></em>*UPDATE*</strong></p>
  
  <p>If I change my HttpPost Action to use FormCollection, I can use the values just fine, I can even re-cast the FormCollection values back to my EmailViewModel.  Why is this?</p>
  
  <pre><code>[HttpPost]
  public ActionResult SendStudentAnEmail(FormCollection emailFormCollection)
  {
          Debug.Write(emailFormCollection["email"]);
          Debug.Write(emailFormCollection["subject"]);
          Debug.Write(emailFormCollection["body"]);
      var email = new EmailViewModel
                          {
                              Email = emailFormCollection["email"],
                              Subject = emailFormCollection["subject"],
                              Body = emailFormCollection["body"]
                          };
  . . . . then the rest of my code works just how I wanted. . . 
  </code></pre>
  
  <p>Why do I have to cast from FormCollection over to my EmailViewModel?  Why isn't it giving me the NullReference Exception if I attempt to simply push an EmailViewModel into my Action?</p>