问题描述
我用我的模型DataType.Date属性和EditorFor在我看来。这在并的,但在它显示一个日期选择器和而不是显示它只是显示月/日/年,在褪去灰色文本的价值。
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:
值:在[RFC 3339]中定义,如果加上有效的完整更新
资格该年组分是四个或更多的数字
重新presenting大于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罚款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!