Asp.net-Mvc

MVC4 DataType.Date EditorFor 不會在 Chrome 中顯示日期值,在 Internet …

  • September 28, 2012

我在我的模型上使用 DataType.Date 屬性,在我的視圖中使用 EditorFor。這在Internet Explorer 8Internet Explorer 9中執行良好,但在Google Chrome中,它顯示了一個日期選擇器,而不是顯示值,它只是以褪色的灰色文本顯示“月/日/年”。

為什麼Google瀏覽器不顯示該值?

模型:

[DataType(DataType.Date)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }

看法:

<td class="fieldLabel">Est. Pur. Date</td>
<td class="field">@Html.EditorFor(m=>m.EstPurchaseDate)</td>

鉻合金

IE瀏覽器

當您在 ASP.NET MVC 4 中使用預設模板裝飾模型屬性時,[DataType(DataType.Date)]會生成一個輸入欄位type="date"

<input class="text-box single-line" 
      data-val="true" 
      data-val-date="The field EstPurchaseDate must be a date."
      id="EstPurchaseDate" 
      name="EstPurchaseDate" 
      type="date" value="9/28/2012" />

支持 HTML5 的瀏覽器(例如​​ Google Chrome)使用日期選擇器呈現此輸入欄位。

為了正確顯示日期,該值的格式必須為2012-09-28. 引用規範

value: [RFC 3339] 中定義的有效完整日期,附加條件是年份部分是四位或更多位,表示大於 0 的數字。

DisplayFormat您可以使用以下屬性強制執行此格式:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> EstPurchaseDate { get; set; }

引用自:https://stackoverflow.com/questions/12633471