MVC4 DataType.Date EditorFor将不会显示在Chrome日期值,在Internet Explorer罚款 [英] MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in Internet Explorer

查看:268
本文介绍了MVC4 DataType.Date EditorFor将不会显示在Chrome日期值,在Internet Explorer罚款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的模型DataType.Date属性和EditorFor在我看来。这在互联网&NBSP做工精细;浏览器  8 并的互联网 &浏览器NBSP; 9 ,但在谷歌Chrome浏览器它显示一个日期选择器和而不是显示它只是显示月/日/年,在褪去灰色文本的价值。

I'm using the DataType.Date attribute on my model and an EditorFor in my view. This is working fine in Internet Explorer 8 and Internet Explorer 9, but in Google Chrome it is showing a date picker and instead of displaying the value it just displays "Month/Day/Year" in faded gray text.

为什么不会谷歌浏览器显示的价值?

Why won't Google Chrome display the value?

型号:

[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>

推荐答案

当你装饰了模型属性 [数据类型(DataType.Date)] 在ASP中的默认模板.NET MVC 4生成的输入字段键入=日期

When you decorate a model property with [DataType(DataType.Date)] the default template in ASP.NET MVC 4 generates an input field of 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这样的谷歌Chrome浏览器,使这个输入域日期选择器。

Browsers that support HTML5 such Google Chrome render this input field with a date picker.

为了正确地显示日期,该值必须格式化为 2012-09-28 。从规范报价:

In order to correctly display the date, the value must be formatted as 2012-09-28. Quote from the specification:

值:在[RF​​C 3339]中定义,如果加上有效的完整更新
  资格该年组分是四个或更多的数字
  重新presenting大于0的数。

value: A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits representing a number greater than 0.

您可以使用 DisplayFormat 属性强制格式为:

You could enforce this format using the DisplayFormat attribute:

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

这篇关于MVC4 DataType.Date EditorFor将不会显示在Chrome日期值,在Internet Explorer罚款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆