MVC日期时间不正确的日期格式结合 [英] MVC DateTime binding with incorrect date format

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

问题描述

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。
例如,在'02 / 09/2009 00:00:00一个DateTime结果提交给一个号召用字符串'09 / 02/2009,或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.

我不希望推出自己的模型粘合剂日期格式的缘故。但似乎不用必须改变行动接受一个字符串,然后使用DateTime.Parse如果MVC是能够做到这对我来说的。

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:

梅尔文港拥有为什么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:

<一个href=\"http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx\">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.   
  3. URI查询字符串

  4.   
  5. 申请表

  6.   

只有最后的这些都将是文化的了解不过。有一个很好的理由,从定位的角度。想象一下,我写这表明我在网上发布航空公司的航班信息的Web应用程序。我期待在某个日期航班通过点击链接的那一天(也许有点像<一个href=\"http://www.melsflighttimes.com/Flights/2008-11-21\">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日期时间不正确的日期格式结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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