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

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

问题描述

Asp.net-MVC现在允许使用DateTime对象的隐式绑定。我有一个行动沿着

  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日。 / p>

为了日期格式,我不想滚动自己的模型binder。但是,如果MVC能够为我做这件事,那么似乎不必要改变接受一个字符串的操作,然后使用DateTime.Parse。



有没有办法更改DateTime的默认模型binder中使用的日期格式?不应该默认的模型binder使用您的本地化设置?

解决方案

我刚刚找到了一些更多的答案详细的搜索:



Melvyn Harbor对于为什么MVC以日期做的方式进行了彻底的解释,如果需要,您可以如何覆盖:



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


当查找要解析的值时,框架将以特定的顺序查找:


  1. RouteData(未显示)

  2. URI查询字符串

  3. 请求表单

只有最后才会有文化意识。从本地化的角度来看,这是一个非常好的理由。想象一下,我已经写了一个网络应用程序,显示我在线发布的航空公司航班信息。我在某个日期查看航班,点击当天的链接(或许像 http ://www.melsflighttimes.com/Flights/2008-11-21 ),然后将该链接的电子邮件发送给我在美国的同事。我们可以保证我们将同时查看同一页数据的唯一方法是使用不变量文库。相比之下,如果我使用表单预订我的航班,一切都在紧张的周期中发生。当CurrentCulture写入表单时,数据可以遵循CurrentCulture,因此在从表单返回时需要遵守。



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

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

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.

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.

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 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 (not shown above)
  2. URI query string
  3. Request form

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天全站免登陆