title: Date Format using Html.DisplayFor() in MVC5
body: <p>Referencing the answer in this <a href="https://stackoverflow.com/questions/17229666/date-formatting-with-html-helpers">post</a> I added /Views/Shared/DisplayTemplates and added a partial view called ShortDateTime.cshtml as shown below:</p>
  
  <pre><code>@model System.DateTime
  @Model.ToShortDateString()
  </code></pre>
  
  <p>When the model contains a value this works and the formatted date is displayed correctly:</p>
  
  <pre><code>@Html.DisplayFor(modelItem =&gt; item.BirthDate, "ShortDateTime")
  </code></pre>
  
  <p>However, if a null value is returned a 'System.InvalidOperationException' is thrown. Indicating:</p>
  
  <blockquote>
    <p>{"The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'."}</p>
  </blockquote>
  
  <p>My first inclination was to use an if statement inside the partial view but it didn't seem to matter. Without referencing the template null values are handled as in:</p>
  
  <pre><code>@Html.DisplayFor(modelItem =&gt; item.BirthDate)
  </code></pre>
  
  <p>but the original issue of formatting remains. When I try to put conditional formatting in the View as follows, it doesn't work but I hoping it's just a syntax thing.</p>
  
  <pre><code>@Html.DisplayFor(modelItem =&gt; item.BirthDate == null) ? string.Empty : (modelItem =&gt; item.BirthDate, "ShortDateTime"))
  </code></pre>
  
  <p>The above results in a different 'System.InvalidOperationException':</p>
  
  <blockquote>
    <p>{"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."}</p>
  </blockquote>
  
  <p>So, is there a way to do conditional formatting in the View to generate just the date from a DateTime value?</p>