Format Date On Binding (ASP.NET MVC)

后端 未结 12 730
感情败类
感情败类 2021-01-31 08:46

In my ASP.net MVC app I have a view that looks like this:

...

<%=Html.TextBox(\"due\")%>
...

I am usi

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 09:14

    Decorate the property in your model with the DataType attribute, and specify that its a Date, and not a DateTime:

    public class Model {
      [DataType(DataType.Date)]
      public DateTime? Due { get; set; }
    }
    

    You do have to use EditorFor instead of TextBoxFor in the view as well:

    @Html.EditorFor(m => m.Due)
    

提交回复
热议问题