title: ASP.NET MVC Null object returned from view
body: <p>I have a very simple issue which probably has a very simple answer, however, I cannot work it out.</p>
  
  <p>I have a view model as below:</p>
  
  <pre><code>namespace CommunicationsLog.ViewModels
  {
      public class CommunicationViewModel
      {
          public Communication Communication { get; set; }
          public Feedback Feedback { get; set; }
          public IEnumerable&lt;Resolution&gt; Resolutions { get; set; }
      }
  }
  </code></pre>
  
  <p>I am attempting to create a new instance of a communication from my Add.cshtml page:</p>
  
  <pre><code>@model CommunicationsLog.ViewModels.CommunicationViewModel
  @{
      ViewBag.Title = "Add";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  @using (Html.BeginForm("Add", "Communication"))
  {
      &lt;div class="form-horizontal" id="addForm"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-12"&gt;
                  @Html.ValidationSummary(true, "", new { @class="text-danger"})
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @if (Model.Communication.Customer.Name == null)
                          {
                              &lt;input id="btnCust" type="button" value="Select Customer" class="btn btn-default addCustomer" /&gt;
                          }
                          else
                          {
                              &lt;span id="custSpan" style="font-size:16px; font:bold;"&gt;
                                  @Html.DisplayFor(model =&gt; model.Communication.Customer.Name)
                                  &lt;input id="btnEditCust" type="button" value="Edit" class="btn btn-default addCustomer"/&gt;
                              &lt;/span&gt;
                          }
                       &lt;div id="addCust"&gt;&lt;/div&gt;
                   &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input id="btnSubmit" type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  &lt;div&gt;
      @Html.ActionLink("Back to Communications", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>On submit this calls the below controller action:</p>
  
  <pre><code>[HttpPost]
      public ActionResult Add(CommunicationViewModel viewModel)
      {
          callEnd = DateTime.Now;
          var totalCallTime = callEnd - callStart;
          Communication insertedComm = null;
          try
          {
              Communication comm = viewModel.Communication;
              comm.CallTime = totalCallTime.ToString(@"mm\:ss");
              var customer = _uow.CustomerRepository.Get()
                                                  .Where(c =&gt; c.Name == comm.Customer.Name);
              comm.CustomerId = customer.FirstOrDefault().CustomerId;
              comm.Customer = customer.FirstOrDefault();
              comm.State = "Open";
              if (ModelState.IsValid)
              {
                  insertedComm = _uow.CommunicationRepository.Insert(comm);
                  _uow.Save();
  
              }
  
              Feedback feedback = viewModel.Feedback;
              if (feedback.Type != null &amp;&amp; feedback.Notes != null)
              {
                  feedback.Communication = insertedComm;
                  feedback.CommunicationId = insertedComm.CommunicationId;
                  var insertedFB = _uow.FeedbackRepository.Insert(feedback);
                  _uow.Save();
              }
  
              return RedirectToAction("Index");
          }
          catch (Exception e)
          {
              if(viewModel.Communication == null)
              {
                  viewModel.Communication = new Communication();
              }                
              return View(viewModel);
          }
      }
  </code></pre>
  
  <p>However, when the viewModel is loaded to the controller action, the communication object is null. The feedback object is successfully instantiated.</p>
  
  <p>After debugging i have worked out that the view model is instantiated correctly and is not null when the page loads and that is shown by pre defined data displaying correctly. It only seems to lose the value on the POST request. </p>
  
  <p>Can anybody help?</p>
  
  <p>Many Thanks,</p>
  
  <p>EDIT:</p>
  
  <p>As pointed out in comments i have actually removed the important part from my snippet so here is the full view excluding the javascript as that is not relevant for the POST request:</p>
  
  <pre><code>@model CommunicationsLog.ViewModels.CommunicationViewModel
  @{
      ViewBag.Title = "Add";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  &lt;h2&gt;Add&lt;/h2&gt;
  &lt;link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"&gt;
  &lt;script src="//code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"&gt;&lt;/script&gt;
  @using (Html.BeginForm("Add", "Communication"))
  {
      &lt;div class="form-horizontal" id="addForm"&gt;
          &lt;h4&gt;Communication&lt;/h4&gt;
          &lt;div id="countdown" class="countdownHolder"&gt;&lt;/div&gt;
          &lt;hr/&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-12"&gt;
                  @Html.ValidationSummary(true, "", new { @class="text-danger"})
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @if (Model.Communication.Customer.Name == null)
                          {
                              &lt;input id="btnCust" type="button" value="Select Customer" class="btn btn-default addCustomer" /&gt;
                          }
                          else
                          {
                              &lt;span id="custSpan" style="font-size:16px; font:bold;"&gt;
                                  @Html.DisplayFor(model =&gt; model.Communication.Customer.Name)
                                  &lt;input id="btnEditCust" type="button" value="Edit" class="btn btn-default addCustomer"/&gt;
                              &lt;/span&gt;
                          }
                          &lt;div id="addCust"&gt;&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Receiver, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.Receiver, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.Receiver, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Department, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.Department, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.Department, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.CommDateTime, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          &lt;span id="custSpan" style="font-size:14px;"&gt;
                              @Html.DisplayFor(model =&gt; model.Communication.CommDateTime)
                          &lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Method, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.Method, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.Method, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Product, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.Product, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.Product, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.PartNo, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.PartNo, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.PartNo, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.Communication.Description, htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.EditorFor(model =&gt; model.Communication.Description, new { htmlAttributes = new { @class = "form-control" } })
                          @Html.ValidationMessageFor(model =&gt; model.Communication.Description, "", new { @class = "text-danger" })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      @Html.Label("Feedback?", htmlAttributes: new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          &lt;input type="checkbox" id="chkFeedback" name="chkFeedback" onclick="showFeedbackForm(this)"/&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div id="feedbackForm" style="display:none"&gt;
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.Feedback.Type, htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
                              @Html.EditorFor(model =&gt; model.Feedback.Type, new { htmlAttributes = new { @class = "form-control" } })
                              @Html.ValidationMessageFor(model =&gt; model.Feedback.Type, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.Feedback.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
                              @Html.EditorFor(model =&gt; model.Feedback.Notes, new { htmlAttributes = new { @class = "form-control" } })
                              @Html.ValidationMessageFor(model =&gt; model.Feedback.Notes, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input id="btnSubmit" type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  &lt;div&gt;
      @Html.ActionLink("Back to Communications", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>I dont know if it helps but here is the full stack trace from VS:</p>
  
  <pre><code>Object reference not set to an instance of an object.       
     at ASP._Page_Views_Communication_Add_cshtml.Execute() in C:\Projects\CommunicationsLog\CommunicationsLog\Views\Communication\Add.cshtml:line 24
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
     at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
     at System.Web.WebPages.StartPage.RunPage()
     at System.Web.WebPages.StartPage.ExecutePageHierarchy()
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
     at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
     at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
     at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
  </code></pre>