http://codego.net/498462/
我如何格式化一个DateTime显示为一天只需在razor生成视图的时间?
c# asp.net-mvc asp.net-mvc-3 日期时间datetime razor
我提供一个完整的位于model.StartDate下面的代码:
@Html.DisplayFor(model => model.StartDate.ToString("hh:mm tt"),
CultureInfo.CreateSpecificCulture("hu-HU"));
我的目标是拿满
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我
本文地址 :CodeGo.net/498462/
-------------------------------------------------------------------------------------------------------------------------
1. 你可以把一个DataAnnotation你的模型类。像这样-> [的DisplayFormat(的DataFormatString=“{0:D}”,ApplyFormatInEditMode=真)]。
2. 创建你的模型的新属性,返回格式化的日期。
public class MyModel
{
// Other fields including your current StartDate
public class StartDateFormatted
{
get
{
return StartDate.ToString("hh:mm tt"),
CultureInfo.CreateSpecificCulture("hu-HU"));
}
}
}
在你的razor标记该属性
@Html.DisplayFor(model => model.StartDateFormatted);
本文标题 :我如何格式化一个DateTime显示为一天只需在razor生成视图的时间?
本文地址 :CodeGo.net/498462/