1

I got this code in my mvc-project:

 @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Massage45min)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Massage30min)
                </td>


            </tr>
        }

Both modelItems are DateTimes. They are now getting displayed like this: 2014-03-23 08:00 and I would like only to display the time. Like this: 08:00.

I thought this would do the trick:

     @Html.DisplayFor(modelItem => item.Massage45min.ToShortTimeString())

But it generates this error:

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

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

Any ideas of how to display the DateTimes the way I want?

Matt Tester
  • 4,663
  • 4
  • 29
  • 32
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • 2
    Possible duplicate of http://stackoverflow.com/questions/6001654/how-to-render-a-datetime-in-a-specific-format-in-asp-net-mvc-3 – Adil Mar 23 '14 at 19:28

1 Answers1

1

One way would be to just print it manually:

<td>
    @item.Message45min.ToShortTimeString()
</td>
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • This answer gives me the result i am after! Is there any negatives when i use these suggestions instead of the @html.Displayfor? Thank you both! – user2915962 Mar 23 '14 at 19:35