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

前端 未结 1 951
灰色年华
灰色年华 2021-02-01 06:31

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.Textbox

相关标签:
1条回答
  • 2021-02-01 06:49

    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 讨论(0)
提交回复
热议问题