ASP.NET MVC 中model含有DateTime類型的字段
更新字段時提示:字段 EditTime必須是日期,、
但是明明填入的是日期還是給出這個提示,
看有的博客說那是因為日期形式錯了,如果填入20130104那是不能通過了,填入01/04/2013就行了
我是用的MVC5,手動輸入“01/04/2013”這樣的格式也是失敗的,但是改成“2016-07-13”這樣的就可以了
從Google找到了幾種解決方法,記錄一下:(原文地址http://stackoverflow.com/questions/1961114/date-only-from-textboxfor)
方法一:
I'm having trouble displaying the only date part of a DateTime into a textbox using TextBoxFor<,>(expression, htmlAttributes).
The model is based on Linq2SQL, field is a DateTime on SQL and in the Entity model.
<%= Html.TextBoxFor(model => model.dtArrivalDate, String.Format("{0:dd/MM/yyyy}", Model.dtArrivalDate))%>
方法二:
[DisplayName("Start Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")] public DateTime StartDate { get; set; }
<%=Html.EditorFor(m => m.StartDate) %>
方法三:
<%= Html.TextBoxFor(model => model.EndDate, new { @class = "jquery_datepicker", @Value = Model.EndDate.ToString("dd.MM.yyyy") })%>