ASP.Net MVC Razor Remove Time From DateTime Property
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have the following ViewModel which is passed to my Razor View and iterated through to display a list of records.

**ViewModel**

    public class ViewModelLocumEmpList
    {
        public IList<FormEmployment> LocumEmploymentList {get; set;}
    }

**View**

    <table>
      <tr>
       <th>Employer</th>
       <th>Date</th>
       </tr>
        @foreach (var item in Model.LocumEmploymentList) {
          <tr>
            <td>@item.employerName</td>
            <td>@item.startDate</td>
          </tr>
          }
          </table>

My problem is that the line

    @Html.DisplayFor(modelItem => item.startDate)

Returns a date like this **20/06/2012 00:00:00**, and I would like it to remove the time and just display the date, ie, **20/06/2012**.

I have tried adding

    @Html.DisplayFor(modelItem => item.startDate.Value.ToShortDateString())

And

    DisplayFor(modelItem => item.startDate.HasValue ? item.startDate.Value.ToShortDateString(): "")



However, they both return the following error message at runtime

    Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I have looked at Darin Dimitrov’s answer here http://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor

However, I don’t have access to the startDate property in my ViewModel, my ViewModel returns an IList of FormEmployment objects which you can see above.

If anyone has any idea’s on how to remove the time from the date time property then I would be greatly appreciative.

Thanks.

Also, my startDate property is Nullable.

ASP.Net MVC Razor Remove Time From DateTime Property
===

I am developing an ASP.Net MVC 3 Web application using Razor Views. I have the following ViewModel which is passed to my Razor View and iterated through to display a list of records.

**ViewModel**

    public class ViewModelLocumEmpList
    {
        public IList<FormEmployment> LocumEmploymentList {get; set;}
    }

**View**

    <table>
      <tr>
       <th>Employer</th>
       <th>Date</th>
       </tr>
        @foreach (var item in Model.LocumEmploymentList) {
          <tr>
            <td>@item.employerName</td>
            <td>@item.startDate</td>
          </tr>
          }
          </table>

My problem is that the line

    @Html.DisplayFor(modelItem => item.startDate)

Returns a date like this **20/06/2012 00:00:00**, and I would like it to remove the time and just display the date, ie, **20/06/2012**.

I have tried adding

    @Html.DisplayFor(modelItem => item.startDate.Value.ToShortDateString())

And

    DisplayFor(modelItem => item.startDate.HasValue ? item.startDate.Value.ToShortDateString(): "")



However, they both return the following error message at runtime

    Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I have looked at Darin Dimitrov’s answer here http://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor

However, I don’t have access to the startDate property in my ViewModel, my ViewModel returns an IList of FormEmployment objects which you can see above.

If anyone has any idea’s on how to remove the time from the date time property then I would be greatly appreciative.

Thanks.

Also, my startDate property is Nullable.
