title: ASP.NET MVC one particular view model property becoming null on returning to view
body: <p>In ASP.NET MVC I have a view that's a form, and I want to be able to save the form and then it returns back to the same page showing the data you entered after saving the data to the database. I'm sure I'm just doing something stupid (this is pretty new to me), but there are some properties that I want to persist and I am setting them on the view model before I return, and I have <code>@Html.HiddenFor</code> inside my view's form. My confusion is that of these items are retained, and some aren't. So I have the following inside my FormController (methods and names have been simplified for brevity):</p>
  
  <pre><code>    public ActionResult Index(int? p, int? c)
          {
              FormViewModel model = new FormViewModel();
              model.p = p;
              model.c = c;
              model.dateStarted = DateTime.Now;
              return View(model);
          }
  
     [HttpPost]
     public ActionResult Index(FormViewModel m)
          {
              Form form;
              bool shouldUpdate = false;
              if (m.formID != null) // m.formID is always null, but m.p, c, dateStarted aren't
              {
                  shouldUpdate = true;
                  form = getFormnWithId((int)m.formID); //gets from database
              }
              else
              {
                  form = new Form(m);
              }
  
              if (shouldUpdate)
              {
                  editForm(form);  //edit existing entry
              }
              else {
                  addForm(form);  //add to database
              }
                  m.formID = form.Id;  // formn.Id is valid because the form has been updated with its Id after being added to the database
                  m.p = form.p;
                  m.c = form.c;
  
                  return View(m);
          }
  </code></pre>
  
  <p>Inside my view (cshtml) file I have <code>@Html.HiddenFor(model=&gt;model.formID)</code> as well as for other properties I want to persist but aren't being set in the form directly.
  The formID however, is not persisting, while the other items (represented by c and p and dateStarted) are fine. If I remove the HiddenFor for those other fields, then they don't work. I click save each time, and formID is null in the post, but it's definitely set after the form has been added to the database and the value of the formID is definitely getting sent to the view. I just don't understand why it comes back null but the other properties don't.</p>
  
  <p>Here's what the model looks like: </p>
  
  <pre><code>public class FormViewModel
  {
       public Nullable&lt;int&gt; formID {get; set;}
       public Nullable&lt;int&gt; c { get; set; }
       public Nullable&lt;int&gt; p { get; set; }
       public System.DateTime dateStarted { get; set; }
       //+ other form properties
  }
  </code></pre>
  
  <p>View:</p>
  
  <p>...</p>
  
  <pre><code>&lt;label for="submit-form" class="btn btn-default"&gt;Save&lt;/label&gt;
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal col-md-12"&gt;
       @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
          &lt;div class="form-group"&gt;
          &lt;!-- various form fields --&gt;
          &lt;/div&gt;
          @Html.HiddenFor(model =&gt; model.dateStarted)
          @Html.DisplayFor(model =&gt; model.dateStarted)&lt;br /&gt;  &lt;!-- just to see it while testing--&gt;
          @Html.HiddenFor(model =&gt; model.c)
          @Html.DisplayFor(model =&gt; model.c)&lt;br /&gt;
          @Html.HiddenFor(model =&gt; model.p)
          @Html.DisplayFor(model =&gt; model.p)&lt;br /&gt;
          @Html.HiddenFor(model =&gt; model.formID)
          @Html.DisplayFor(model =&gt; model.formID)
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Save" name="Command" class="btn btn-default hidden" id="submit-form" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  }
  </code></pre>