我想正确显示一个(可为空)仅Date-type-only字段。对于这个领域,我的模型和视图模型类被定义为....排除时间从TextBoxFor当ViewModel设置为DataType的日期
[DisplayFormat(DataFormatString = "{0:M/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime? PeriodEnd { get; set; }
在我的详细信息视图(基于模型),它显示的日期正确(不包括时间因素):
@Html.DisplayNameFor(model => model.PeriodEnd)
问题:在我的编辑视图中(基于ViewModel),它显示了时间,我试图排除。这是如何定义的。
@Html.TextBoxFor(model => model.PeriodEnd, new { @class = "datepicker" })
我也试过....
@Html.TextBoxFor(model => model.PeriodEnd.Value().ToShortDateString(),
new { @class = "datepicker" })
...但产生的错误。
有没有更好的方法来实现这个目标?
可能相关:http://stackoverflow.com/questions/11179027/asp-net-mvc-3-remove-time-from-date –