title: Nullable DateTime in Html Helper?
body: <p>I have an ASP.NET MVC application where I am trying to add a field to edit a date. The date is a nullable DateTime object. What I was trying to do was create 3 input boxes (Month, Day, and Year) and have them be linked to the model. I want it to display the date components in those boxes when there is a date set. When there's not, it should not have any value and just display the placeholder.</p>
  
  <p><a href="https://i.stack.imgur.com/pUER9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pUER9.png" alt="enter image description here"></a></p>
  
  <p>I'm using html helpers to do this (though this is not a requirement), but I cannot get it to work when the date is null.</p>
  
  <p>Here's what I have currently:</p>
  
  <pre><code>@Html.EditorFor(model =&gt; model.Active_Date_Start.Value.Month, new { htmlAttributes = new { @class = "form-control active-date", @placeholder = "MM", @maxlength = "2" } }) /
  @Html.EditorFor(model =&gt; model.Active_Date_Start.Value.Day, new { htmlAttributes = new { @class = "form-control active-date", @placeholder = "DD", @maxlength = "2" } }) /
  @Html.EditorFor(model =&gt; model.Active_Date_Start.Value.Year, new { htmlAttributes = new { @class = "form-control active-date", @placeholder = "YYYY", @maxlength = "4" } })
  </code></pre>
  
  <p>This works when the date is not null, but causes an error when it is.</p>
  
  <p>The other approach I tried was to use pure html to create these input boxes, and then assemble the date myself on the backend when the form is submitted. I couldn't get this to work because I couldn't figure out how to conditionally set the value of the inputs.</p>
  
  <p>Any suggestions on how to do this would be greatly appreciated. Thanks!</p>