我在以下代码中提供位于 model.StartDate 的完整日期时间:
@Html.DisplayFor(model => model.StartDate.ToString("hh:mm tt"),
CultureInfo.CreateSpecificCulture("hu-HU"));
我的目标是获取完整的 DateTime
8/30/2012 11:33:48 AM
仅显示
11:33:48 AM
但是,我收到以下错误:
Templates can be used only with field access, property access,
single-dimension array index, or single-parameter custom indexer expressions.
为什么会出现这个错误?
注意:我引用了此文档来为我的日期时间格式化我的 .ToString:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
最佳答案
您可以在模型类上放置一个 DataAnnotation。像这样的东西 -> [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)].
关于c# - 如何格式化 DateTime 以在 Razor 生成的 View 中仅显示为一天中的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12201986/