title: ASP.NET MVC 5 prevent value changes
body: <p>When I update my website, it hints me this problem "{"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}"</p>
  
  <p>The screenshot is list below, there is a value named RecordDate, it has value, but I will not change anything about that value so I didn't display it on the screen.</p>
  
  <p>The problem is MVC automatically update that value for me, and the value of the date becomes 0000-00-01 i think, maybe something else, how to prevent it? just keep the origin value and update other columns.</p>
  
  <p><img src="https://i.stack.imgur.com/VyiFG.png" alt="{&quot;The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.&quot;}"></p>
  
  <p>The model class looks like this</p>
  
  <pre><code> public class ShiftRecord
  {
      public int ID { get; set; }
      public int EmployeeID { get; set; }
      [Display(Name="Company Vehicle?")]
      [UIHint("YesNo")]
      public bool IsCompanyVehicle { get; set; }
      [Display(Name="Own Vehicle?")]
      [UIHint("YesNo")]
      public bool IsOwnVehicle { get; set; }
  
  
      //Problem comes from this line
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString="{0:yyyy-MM-dd}")]
      public DateTime RedordDate { get; set; }
      [Display(Name="Day Type")]
      public Nullable&lt;DayType&gt; DayType { get; set; }
      [Display(Name="Normal Hrs")]
      public Nullable&lt;int&gt; NormalHours { get; set; }
      [Display(Name="Time and Half Hrs")]
      public Nullable&lt;int&gt; TimeAndHalfHours { get; set; }
      [Display(Name="Double Time Hrs")]
      public Nullable&lt;int&gt; DoubleTimeHours { get; set; }
      [Display(Name="Shift Hrs")]
      public Nullable&lt;int&gt; ShiftHours { get; set; }
      public string Comment { get; set; } // System manager can leave any comment here
  
      public bool IsRead { get; set; } // has this shift record been read
  
      public virtual Employee Employee { get; set; }
      public virtual ICollection&lt;JobRecord&gt; JobRecords { get; set; }
  
  }
  </code></pre>
  
  <p>In the controller, I didn't change anything about the model, so it looks like this:</p>
  
  <pre><code>      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include = "ID,EmployeeID,IsCompanyVehicle,IsOwnVehicle,RecordDate,DayType,NormalHours,TimeAndHalfHours,DoubleTimeHours,ShiftHours,Comment,IsRead")] ShiftRecord shiftrecord)
      {
          if (ModelState.IsValid)
          {
              db.Entry(shiftrecord).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "LastName", shiftrecord.EmployeeID);
          return View(shiftrecord);
      }
  </code></pre>
  
  <p>And I didn't change Edit view as well, the only thing is I made RecordDate unchangeable, changed it from @Html.EditorFor to @Html.DisplayFor</p>
  
  <pre><code>&lt;div class="form-group"&gt;
                      @Html.LabelFor(model =&gt; model.RedordDate, new { @class = "control-label col-md-2" })
                      &lt;div class="col-md-10"&gt;
                          @Html.DisplayFor(model =&gt; model.RedordDate)
                          @Html.ValidationMessageFor(model =&gt; model.RedordDate)
                      &lt;/div&gt;
                  &lt;/div&gt;
  </code></pre>