全球化问题,DateTime和ASP.NET MVC 3模型绑定 [英] Globalization problem with DateTime and ASP.NET MVC 3 Model Binding

查看:107
本文介绍了全球化问题,DateTime和ASP.NET MVC 3模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序下,RO-RO文化的设置,在web.config中的全球化节配置运行。如果我作出这样一个POST请求

My application runs under ro-RO culture settings, configured in web.config globalization section. If I make a POST request like

POST /myapp/index
date=03-12-2010&value=something

在模型绑定映射此更正2010年12月03的日期值,因为对RO-RO文化的默认日期时间格式为DD-MM-YYYY。如果我改变请求方法来获取传入我的行动的日期值变为相同的数据2010年3月12(MM-DD-YYYY日期时间格式)

the model binding maps this to correct date value of "03 December 2010", since the default datetime format for ro-RO culture is dd-MM-yyyy. If I change the request method to GET passing the same data the date value in my action becomes "12 March 2010" (MM-dd-yyyy datetime format)

GET /myapp/index?date=03-12-2010&value=something

$.getJSON('/Home/Index', $('form').serialize(), function(d) {
            // ...
});
$.post('/Home/Index', $('form').serialize(), function(d) {
            // ...
}, 'json');

因此​​,在这种情况下的getJSON与& 后必须返回相同的结果,但我得到,因为日期时间差不同的结果。

So in this case "getJson" & "post" must return the same result, but I get different results because of datetime difference.

我怎样才能使同样的解析格式GET请求也?

How can I enable the same parsing format for GET requests also?

我知道我可以用一个更通用的格式,如YYYY-MM-DD的日期,但我只是好奇,为什么会出现这种情况?

I know I can use a more generic format such as yyyy-MM-dd for dates, but I am just curious why is this happening?

推荐答案

从对这个问题我的previous答案:<一href=\"http://stackoverflow.com/questions/3753569/nullable-datetime-parameter-is-never-bound-when-calling-the-action/3753829#3753829\">Nullable日期时间参数是从来没有必然调用动作时

From my previous answer on this subject: Nullable DateTime Parameter is never bound when calling the action

<青霉>这是有意的。凡是是URI的一部分(注意在URI中的'统一')是PTED就好像它是从固定区域性来除$ P $。这是这样,在用户谁副本的链接,并将其发送过IM在英国的朋友可以确信,他的朋友会看到完全一样的页面(而不是一个HTTP 500由于日期时间转换错误,例如)美国一般来说,日期,或的RouteData查询字符串传递应该在格式YYYY-MM-DD,以便跨文化不含糊

如果您需要跨preT的文化感知方式查询字符串或参数的RouteData,在把它作为一个字符串,然后将其转换到手动所需的类型,传入所需的文化。 (DateTime.Parse有重载允许你指定的文化。)如果你这样做,我建议也正在采取所需的文化作为查询字符串或一个的RouteData参数,以便URI的'统一'的一部分不丢失,例如该网址看起来像......文化= FR-FR&放大器;?日期= 1990年1月10日

这篇关于全球化问题,DateTime和ASP.NET MVC 3模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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