title: EditorFor on nullable DateTime - "Nullable object must have a value."
body: <p>I am attempting to display a form that allows a user to input a new assignment for a person.  I'm using a DateTime.cshtml EditorTemplate to handle DateTime values for the assignment.  The non-nullable DateTime works fine.  The nullable DateTime causes an "InvalidOperationException: Nullable object must have a value." </p>
  
  <p>I have a simple viewmodel that looks like this:</p>
  
  <p>AssignmentViewModel.cs:</p>
  
  <pre><code>public Person Person { get; set; }
  public Assignment NewAssignment { get; set; }
  </code></pre>
  
  <p>Assignment.cs contains:</p>
  
  <pre><code>public DateTime AssignmentStartDate { get; set; }
  public DateTime? AssignmentEndDate { get; set; }
  </code></pre>
  
  <p>My AssignmentController Create() method looks like:</p>
  
  <pre><code>public ViewResult Create(int personId)
  {
      Person person = personRepository.GetPersonById(personId);
      var newAssignment = new AssignmentViewModel { Person = person, NewAssignment = new Assignment() };
      return View(newAssignment);
  }
  </code></pre>
  
  <p>My Create.cshtml view looks like this:</p>
  
  <pre><code>@model AssignmentViewModel
  
  @using (Html.BeginForm("Create", "Assignment"))
  {
      @Html.Hidden("NewAssignment.PersonId", Model.Person.PersonId)
      @Html.LabelFor(x =&gt; x.NewAssignment.AssignmentStartDate):
      @Html.EditorFor(x =&gt; x.NewAssignment.AssignmentStartDate.Date, new { cssClass = "datePicker" })
      &lt;br /&gt;
      @Html.LabelFor(x =&gt; x.NewAssignment.AssignmentEndDate):
      @Html.EditorFor(x =&gt; x.NewAssignment.AssignmentEndDate.Value.Date, new { cssClass = "datePicker" })
      &lt;br /&gt;
      &lt;input type="submit" value="Send /&gt;
  }
  </code></pre>
  
  <p>My DateTime.cshtml EditorTemplate looks like:</p>
  
  <pre><code>@model DateTime?
  
  @{
      String modelValue = "";
      if (Model.HasValue)
      {
          if (Model.Value != DateTime.MinValue)
          {
              modelValue = Model.Value.ToShortDateString();
          }
      }
  }
  
  @Html.TextBox("", modelValue, new { @class = "datePicker" })
  </code></pre>
  
  <p>When I attempt to load the Create view, I get the exception mentioned above on the line "@Html.EditorFor(x => x.NewAssignment.AssignmentEndDate.Value)".</p>
  
  <p>You may be wondering why I'm passing in AssignmentEndDate.Value.Date instead of just passing in AssignmentEndDate; the reason is because I'm trying to get to the point where I'm splitting DateTime into Date and a TimeOfDay field and recombine them with a DateTimeModelBinder.  I am using a similar technique to the one <a href="http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx" rel="nofollow">shown here</a> and <a href="http://devblog.lundy.us/2010/09/28/date-time-picker-asp-net-mvc-jquery-part-1/" rel="nofollow">here</a>.</p>
  
  <p>I -can- bypass the error, by changing my controller Create() method to instantiate the ViewModel with AssignmentEndDate set to DateTime.MinValue, but this seems completely wrong for a nullable DateTime:</p>
  
  <pre><code>var newAssignment = new AssignmentViewModel 
                          { 
                              Person = person, 
                              NewAssignment = new Assignment { AssignmentEndDate = DateTime.MinValue } 
                          };
  </code></pre>
  
  <p>Something strange happens after I "bypass" the error by supplying a value for the nullable DateTime; the un-required nullable DateTime property (AssignmentEndDate.Date) fails client side validation.  Trying to submit the form highlights the field in red.</p>
  
  <p>How can I handle this correctly?</p>