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") })%>