Asp.Net MVC 2 Html.TextBoxFor Best way to specify a format string for a DateTime property of model

依然范特西╮ 提交于 2019-12-03 04:59:43

问题


As the question title explains, what is the best way to specify a format which my Views use when displaying values of a DateTime property via the Html.TextboxFor method.

The default display includes the Date and Time where I really only want the Date displayed.

Thanks


回答1:


In your model definition add the DisplayFormatAttribute to the DateTime property:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }

Then in your View call:

<%=Html.EditorFor(m => m.Date)%>

If all goes well you should get a TextBox with the value filled in in the format specified in the attribute definition.



来源:https://stackoverflow.com/questions/2869918/asp-net-mvc-2-html-textboxfor-best-way-to-specify-a-format-string-for-a-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!