MVC DateTime 绑定日期格式不正确 [英] MVC DateTime binding with incorrect date format

查看:27
本文介绍了MVC DateTime 绑定日期格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.net-MVC 现在允许隐式绑定 DateTime 对象.我有一个类似

Asp.net-MVC now allows for implicit binding of DateTime objects. I have an action along the lines of

public ActionResult DoSomething(DateTime startDate) 
{ 
... 
}

这成功地将字符串从 ajax 调用转换为 DateTime.但是,我们使用日期格式 dd/MM/yyyy;MVC 正在转换为 MM/dd/yyyy.例如,使用字符串09/02/2009"提交对操作的调用会导致 DateTime 为02/09/2009 00:00:00",或者在我们的本地设置中为 9 月 2 日.

This successfully converts a string from an ajax call into a DateTime. However, we use the date format dd/MM/yyyy; MVC is converting to MM/dd/yyyy. For example, submitting a call to the action with a string '09/02/2009' results in a DateTime of '02/09/2009 00:00:00', or September 2nd in our local settings.

我不想为了日期格式而滚动我自己的模型活页夹.但是,如果 MVC 能够为我执行此操作,则似乎不必更改操作以接受字符串然后使用 DateTime.Parse.

I don't want to roll my own model binder for the sake of a date format. But it seems needless to have to change the action to accept a string and then use DateTime.Parse if MVC is capable of doing this for me.

有什么方法可以更改 DateTime 的默认模型绑定器中使用的日期格式?无论如何,默认模型绑定器不应该使用您的本地化设置吗?

Is there any way to alter the date format used in the default model binder for DateTime? Shouldn't the default model binder use your localisation settings anyway?

推荐答案

我刚刚通过更详尽的谷歌搜索找到了这个问题的答案:

I've just found the answer to this with some more exhaustive googling:

Melvyn Harbor 详细解释了为什么 MVC 以这种方式处理日期,以及如何在必要时覆盖它:

Melvyn Harbour has a thorough explanation of why MVC works with dates the way it does, and how you can override this if necessary:

http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

在查找要解析的值时,框架按特定顺序查找,即:

When looking for the value to parse, the framework looks in a specific order namely:

  1. RouteData(上面未显示)
  2. URI 查询字符串
  3. 申请表

然而,只有最后一个是文化意识.从本地化的角度来看,这是有充分理由的.想象一下,我编写了一个 Web 应用程序,显示我在线发布的航空公司航班信息.我通过单击当天的链接来查找特定日期的航班(可能类似于 http://www.melsflighttimes.com/Flights/2008-11-21),然后想将该链接通过电子邮件发送给我在美国的同事.我们可以保证我们将查看同一页数据的唯一方法是使用 InvariantCulture.相比之下,如果我使用表格来预订我的航班,那么一切都在一个紧凑的循环中发生.数据写入表单时可以尊重CurrentCulture,因此从表单返回时需要尊重它.

Only the last of these will be culture aware however. There is a very good reason for this, from a localization perspective. Imagine that I have written a web application showing airline flight information that I publish online. I look up flights on a certain date by clicking on a link for that day (perhaps something like http://www.melsflighttimes.com/Flights/2008-11-21), and then want to email that link to my colleague in the US. The only way that we could guarantee that we will both be looking at the same page of data is if the InvariantCulture is used. By contrast, if I'm using a form to book my flight, everything is happening in a tight cycle. The data can respect the CurrentCulture when it is written to the form, and so needs to respect it when coming back from the form.

这篇关于MVC DateTime 绑定日期格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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