ASP.NET MVC的显示格式无法将DateTime格式化为仅日期字符串。

4
我已经设置了我的显示格式,如下所示:
<DisplayName("birthdate")> _
<DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0:MM/dd/yyyy}")> _
Public Property BirthDate As DateTime

然后在视图中我有以下内容:

<%: Html.TextBoxFor(Function(model) model.BirthDate, Model.BirthDate)%>
<%: Html.ValidationMessageFor(Function(model) model.BirthDate) %>

这意味着输出“应该”是

1982年6月24日

但不幸的是,输出为

1982年6月24日 12:00:00 AM

有人知道我可能漏了什么吗?


我不知道VB.NET的lambda语法,但我认为你的TextBoxFor中的第二个参数可能是多余的 - 它可能会将其解释为HTML属性对象。但是,这不会导致你在这里遇到的问题。 - Rup
1个回答

6

从Codeplex挖掘MVC2源代码,我最好的猜测是你需要使用EditorFor而不是TextBoxFor。TextBoxFor使用

string valueParameter = Convert.ToString(value, CultureInfo.CurrentCulture);

忽略格式属性(尽管通过 ModelMetadata 类查找了它们),而 EditorFor 和 DisplayFor 是构建在内部 TemplateHelpers 上的,这些辅助方法确实使用格式属性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接