我在mvc-project中得到了这段代码:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Massage45min)
</td>
<td>
@Html.DisplayFor(modelItem => item.Massage30min)
</td>
</tr>
}
两个modelItem
都是DateTimes。它们现在显示如下:2014-03-23 08:00
,我只想显示时间。像这样:08:00
。
我认为这样可以解决问题:
@Html.DisplayFor(modelItem => item.Massage45min.ToShortTimeString())
但它会产生此错误:
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.
如何以我想要的方式显示DateTimes的想法?
答案 0 :(得分:1)
一种方法是手动打印:
<td>
@item.Message45min.ToShortTimeString()
</td>