MVC DateTime 字段 EditTime 必須是日期模板只能用於字段訪問、屬性訪問、一維數組索引或單參數自定義索引器表達式


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM