开发者

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

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 defa开发者_Python百科ult display includes the Date and Time where I really only want the Date displayed.

Thanks


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜