日期不一致的跨浏览器 - 从模型MVC 5日 [英] Date inconsistency across browsers - MVC 5 date from model

查看:100
本文介绍了日期不一致的跨浏览器 - 从模型MVC 5日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的一个视图模型:

I have this in a view model:

[Required]
[Display(Name = "Date of birth")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }

和的形式:

<div class="form-group">
      @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
             @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } })
             @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
      </div>
</div>

在除了Chrome的所有浏览器只显示DD / MM / YYYY,而不是从模型中值填充字段的很好的作品。

Works nicely in all browsers apart from Chrome which only displays "dd/MM/yyyy" instead of populating the field with the value from the model.

于是,我试图改变在视图模型的日期格式为:

So I tried changing the date format in the ViewModel:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

浏览器开始显示/ MM / YYYY(这可能是由于我的本地区域设置?)格式为dd模式的价值,但所有其他浏览器开始显示YYYY-MM-DD格式。

Chrome starts showing the model value in the format dd/MM/yyyy (potentially due to my local region settings?), but all the other browsers start showing the yyyy-MM-dd format.

我得到的,它是一个国际公认的标准,但作为一个澳大利亚网站与我们进行合作设计是DD / MM / YYYY的格式。

I get that it's an international recognised standard, but being an Australian website the design we were working with was in the format of dd/MM/yyyy.

我能得到它的工作,使所有的浏览器显示DD / MM / YYYY,而无需诉诸:

Can I get it working so that all the browsers show dd/MM/yyyy without needing to resort to:

@Html.TextBoxFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" }, @Value = Model.DateOfBirth.ToString("dd/MM/yyyy") })

这意味着Chrome不提供日期选择器功能。

Which means that Chrome doesn't provide the date picker functionality.

使用最新版本的MVC的,与C#视图模型。看起来像歌剧的行为类似于Chrome浏览器。

Using latest version of MVC, with a c# view model. Looks like Opera behaves similarly to Chrome.

推荐答案

我们已经结束了仅仅使用单独的下拉为日,月,年字段列表。我想象中的另一种选择将是地发现,在所有的浏览器(和移动设备)工作了日期选择器插件。烦!

We've ended up just using separate drop down lists for the day, month, year fields. I imagine the other option would have been to find a datepicker plugin that worked in all browsers (and on mobile devices). Annoying!

这篇关于日期不一致的跨浏览器 - 从模型MVC 5日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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