草庐IT

c# - 格式化日期时间错误 "Templates can be used only with field access, property access, single-dimension array index.."

coder 2024-05-31 原文

在 MVC Razor View 中,我试图将 DateTime 字段格式化为仅显示时间。使用下面的代码我收到错误“模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。”

<td>@(Html.DisplayFor(m=>row.LastUpdatedDate.ToString("HH:mm:ss")))</td>

任何帮助请问是什么导致了这个错误以及如何解决它?

感谢您的帮助。

最佳答案

我遇到了同样的问题,我已经解决了。 如果您想将“LastUpdatedDate”转换为特定格式,那么您可以试试这个:

@Html.TextBoxFor(m=>row.LastUpdatedDate, 
    new { @Value = Convert.ToString(row.LastUpdatedDate.ToShortDateString()) })

关于c# - 格式化日期时间错误 "Templates can be used only with field access, property access, single-dimension array index..",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417311/

有关c# - 格式化日期时间错误 "Templates can be used only with field access, property access, single-dimension array index.."的更多相关文章

随机推荐