看起来粘合剂日期asp.net的MVC工作不正确 [英] Looks like binder for date works incorrect in asp.net mvc

查看:232
本文介绍了看起来粘合剂日期asp.net的MVC工作不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开始日期与查询字符串值:2012/3/1

DateTime.Parse(的Request.QueryString [开始日期])个月回报月份数字:1。

但在我的控制,我有行动指数(日期时间的startDate)和startDate.Month回报3

是任何人都可以解释为什么预期日期不工作的绑定?

BTW,我有文化在web.config中已经:

 <全球化的UICulture =EN-GB文化=EN-GB/>


解决方案

默认的模型绑定的总是解析查询字符串值时使用InvarianCulture,无论你在你的web.config配置它的文化。


  • GET => InvariantCulture的

  • POST =>文化无关

因此​​,假如你有2个动作:

  [HTTPGET]
公众的ActionResult美孚(DateTime的日期)
{
    ...
}[HttpPost]
公众的ActionResult酒吧(DateTime的日期)
{
    ...
}

当您调用美孚行动应始终使用固定区域性格式化日期查询字符串,而当你调用律师的行动,并通过在POST身体有效载荷的日期参数,则默认模型绑定将使用在配置你的web.config文化。

看看在以下博客文章覆盖此的更多细节。

I have startdate in QueryString with value: 3/1/2012

DateTime.Parse(Request.QueryString["startdate"]).Month return month number: 1

but in my controller i have action Index(DateTime startDate) and startDate.Month return 3

Is anybody can explain why bind of date doesnt work as expected?

btw, i have culture in web.config already:

<globalization uiCulture="en-GB" culture="en-GB"/>

解决方案

The default model binder always uses InvarianCulture when parsing query string values, no matter which culture you configured in your web.config.

  • GET => InvariantCulture
  • POST => culture agnostic

So assuming you have the 2 actions:

[HttpGet]
public ActionResult Foo(DateTime date)
{
    ...
}

[HttpPost]
public ActionResult Bar(DateTime date)
{
    ...
}

when you invoke the Foo action you should always use the invariant culture to format the date in the query string, whereas when you invoke the Bar action and pass the date parameter in the POST body payload, the default model binder will use the culture configured in your web.config.

Take a look at the following blog post which covers this in more details.

这篇关于看起来粘合剂日期asp.net的MVC工作不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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