未为 ModelState.Value.Culture 设置 MVC3 全球化

发布于 2024-11-18 22:28:32 字数 796 浏览 7 评论 0原文

我已在 Action Filer 中将文化设置为

Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

文化 = {fr-be} => 法国比利时。

仅供参考,此操作过滤器根据用户选择设置文化。

在 myAction 之一中,用户以 [dd/mm/yyyy] => 格式输入日期2011 年 7 月 26 日。 操作如下

    public ActionResult RequestVacation(VacationRequest model)
    {
        if(ModelState.IsValid)
        {

......

当我调试代码模型时。VacationDate 包含 01/01/0001 ;虽然应该是 7/26/2011 而 Form[VacationDate] 包含 26/07/2011 [采用 Fr-BE 甲酸盐] 并且 ModelState.IsValid 为 false;尽管它应该是正确的,因为 fr-be 格式的日期是正确的。 当我进一步挖掘但在视觉工作室检查当地人时 我发现

this.ModelState[1].Culture = {en-US}

我已经使用 actionFilter 设置了文化值,如上所述。 我的问题是如何设置 this.ModelState.Culture = {fr-be}?

i have set culture in Action Filer as

Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

where culture = {fr-be} =>French Belgium.

FYI, this action filter sets the culture on user choice.

in one of myAction user is entering Date in format [dd/mm/yyyy] => 26/7/2011.
Action is as follows

    public ActionResult RequestVacation(VacationRequest model)
    {
        if(ModelState.IsValid)
        {

....

when i dubug the code model.VacationDate contains 01/01/0001 ; although it should be 7/26/2011
whereas Form[VacationDate] contains 26/07/2011 [which is in Fr-BE formate]
And ModelState.IsValid is false; although it should be true, as date is correct in fr-be format.
when i furtur dig out but checking locals in visual studio
i found

this.ModelState[1].Culture = {en-US}

whereas i had already set culture value using actionFilter, as stated above.
My question is how can i set this.ModelState.Culture = {fr-be}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北陌 2024-11-25 22:28:32

回答你的问题有点晚了,但我想发生的事情是你在操作过滤器的 OnActionExecuting 方法中设置当前线程区域性,但模型绑定发生在这之前,所以它不会接受你的更改。

在模型绑定发生之前,您需要将设置区域性的代码移动到管道中较早的位置,例如在 Application BeginRequest 方法中,或者如果需要在会话中存储区域性,则在 AcquireRequestState 中。

A bit late to answer your question but I imagine that what is happening is you are setting the current thread culture in the OnActionExecuting method of an action filter, but model binding happens prior to that so it doesn't pick up your change.

You'll need to move your code that sets the culture to earlier on in the pipeline, before model binding occurs, for example in the Application BeginRequest method, or AcquireRequestState if you need to store the culture in session.

灼疼热情 2024-11-25 22:28:32

将以下内容放入您的 web.config 中:

<configuration>
   <system.web>
      <globalization
           fileEncoding="utf-8"
           requestEncoding="utf-8"
           responseEncoding="utf-8"
           culture="fr-BE"
           uiCulture="fr-BE"
        />
   </system.web>
</configuration>

Put the following in your web.config:

<configuration>
   <system.web>
      <globalization
           fileEncoding="utf-8"
           requestEncoding="utf-8"
           responseEncoding="utf-8"
           culture="fr-BE"
           uiCulture="fr-BE"
        />
   </system.web>
</configuration>
双马尾 2024-11-25 22:28:32

为了回答我的上述问题,我已经以这种方式解决了它,

            if (ModelState.Keys.Contains("VactionDate"))
        {
            ModelState err = ModelState["VactionDate"];
            if (!err.Value.Culture.Equals(Thread.CurrentThread.CurrentCulture))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(err.Value.AttemptedValue, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
                    model.VactionDate = dt;
                    ModelState.Remove("VactionDate");
                }
                catch
                {
                }
            }
        }

我知道这不是一个好的解决方案。但我仍在寻找某种方法来更改,在验证发生之前,

ModelState[n].Value.Culture = {en-US}

ModelState[n].Value.Culture = {fr-BE}

其中 {fr-BE} 是我所需的文化,以便解析日期时间。
所以我仍在寻找有人为此找到一个好的解决方案。

In response to my above question i had solved it in this way

            if (ModelState.Keys.Contains("VactionDate"))
        {
            ModelState err = ModelState["VactionDate"];
            if (!err.Value.Culture.Equals(Thread.CurrentThread.CurrentCulture))
            {
                try
                {
                    DateTime dt = Convert.ToDateTime(err.Value.AttemptedValue, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
                    model.VactionDate = dt;
                    ModelState.Remove("VactionDate");
                }
                catch
                {
                }
            }
        }

i know this is not a good solution. but i m still looking for some way to change, before validation occurs,

ModelState[n].Value.Culture = {en-US}

To

ModelState[n].Value.Culture = {fr-BE}

where {fr-BE} is my required Culture, for dateTime to be parsed.
so i m still looking for someone to find out a good solution for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文