title: Date format not working in MVC with Entity Framework?
body: <p>I'm trying to get some data to format in the ShortDateTime format of "MM/dd/yyyy" but everything I'm trying is not working. </p>
  
  <p>I have the following EF below and the field I'm specifically looking at is the ExpireDate. When I try to use a DateTime.Parse around the field I get an error <code>cannot convert from 'System.DateTime?" to 'string'</code>. Without the DateTime.Parse then the date displays as <code>3/15/2017 12:00:00 AM</code>.</p>
  
  <p>I tried using Data Annotations on the Model as well but that doesn't seem to work either. </p>
  
  <p>Any suggestions?</p>
  
  <p>EF query</p>
  
  <pre><code>data = (from e in ctx.clock_emp_excep_hist
          join g in ctx.clock_group on e.group_id equals g.group_id
          join emp in ctx.employee on e.emp_id equals emp.emp_id
          where e.group_id == 4
          select new 
          {
              UpdateDate = e.create_date,
              UpdateType = e.add_delete_ind,
              GroupId = e.group_id,
              GroupName = g.group_name,
              Reason = e.reason,
              ExpireDate = e.expiration_date,
              UpdateEmployeeId = e.emp_id,
              UpdateFirstName = emp.emp_firstname,
              UpdateLastName = emp.emp_lastname
          }).Select(e =&gt; new EmployeeExceptionLog {
                      UpdateDate = e.UpdateDate,
                      UpdateType = e.UpdateType == "A" ? "Add" : "Delete",
                      GroupId = e.GroupId,
                      GroupName = e.GroupName,
                      Reason = e.Reason,
                      ExpireDate = DateTime.Parse(e.ExpireDate),
                      UpdateEmployeeId = e.UpdateEmployeeId,
                      UpdateFirstName = e.UpdateFirstName,
                      UpdateLastName = e.UpdateLastName
          }).ToList();
      }
  </code></pre>
  
  <p>Property in class</p>
  
  <pre><code>[DataType(DataType.DateTime)]
  [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode=true)]
  public DateTime? ExpireDate { get; set; }
  </code></pre>
  
  <p><strong>More info:</strong></p>
  
  <p>This is how I'm displaying the data in the View.</p>
  
  <pre><code>@foreach (var item in Model.AuditLogs)
  {
      &lt;tr class="@(item.UpdateType == "Delete" ? "danger" : "")"&gt;
          &lt;td&gt;@item.UpdateDate&lt;/td&gt;
          &lt;td&gt;@item.UpdateType&lt;/td&gt;
          &lt;td&gt;@item.Reason&lt;/td&gt;
          &lt;td&gt;@item.ExpireDate&lt;/td&gt;
          &lt;td&gt;@item.UpdateEmployeeId - @item.UpdateFirstName @item.UpdateLastName&lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p>The DataType in the Oracle Database is DateTime?.</p>