【发布时间】:2010-09-10 10:55:25
【问题描述】:
我的模型中有一个带有 BornDate 属性的 Person 类,定义为
[DisplayName("Born Date")]
public DateTime? BornDate { get; set; }
我在视图中将此字段用作
<td style="white-space:nowrap">
<%= Html.LabelFor(model => model.BornDate)%>
<br />
<%= Html.TextBoxFor(model => model.BornDate, new { id = "bornDate" })%>
<%= Html.ValidationMessageFor(model => model.BornDate, "*")%>
</td>
问题是当我使用 BornDate 文本框编辑 Person 实例时,其格式为
dd/MM/yyyy hh.mm.ss
虽然我想在没有时间部分的情况下格式化它(“dd/MM/yyyy”)。我无法将 toString 方法与格式字符串一起使用,因为它是一个可为空的字段。
我能做什么?
【问题讨论】:
-
这里也有很好的例子stackoverflow.com/questions/12633471
-
关于同样问题的好例子可以在这里找到stackoverflow.com/questions/12633471
标签: asp.net-mvc asp.net-mvc-2 defaultmodelbinder