MVC 4日期间的文化问题? [英] MVC 4 date culture issue?

查看:168
本文介绍了MVC 4日期间的文化问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 4,我想知道是否有在日期时间信息文化的一个bug?

I am using MVC 4 and am wondering if there is a bug in the Datetime culture info?

我试图让澳大利亚日期的工作(DD / MM / YYYY),但它口口声声说的日期格式是错误的,甚至puttig一个globalizaton到web.config后。我认为这是我的code错误,但即使你开始一个新项目,它仍然发生。

I am trying to get Australian dates to work (dd/MM/yyyy), but it keeps saying that the date format is wrong, even after puttig a globalizaton to the web.config. I thought it was an error with my code, but even if you start a new project it still happens.

我开始了新的MVC 4 Web应用程序。

I started a new MVC 4 Web application.

增加了以下到web.config文件

Added the following to the web.config file

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

然后,我添加了下面到的 AccountModels.cs 的文件:

[DataType(DataType.DateTime)]
[Required(ErrorMessage="Date is required")]
public DateTime? MyDate { get; set; }

然后,我添加了下面到的 Register.cshtml 的文件:

<li>
    @Html.LabelFor(m => m.MyDate)
    @Html.TextBoxFor(m => m.MyDate)
</li>

运行应用程序,进入注册页面,尝试像26/03/2013日期和它说不是一个有效的格式。

Run the application, go to the register page, try a date like 26/03/2013 and it says not a valid format.

请帮忙。

推荐答案

尝试添加这个属性,在数值指明MyDate 属性:

Try adding this attribute to your MyDate property:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

虽然在制定文化的的web.config 的应该这样做,这应该强制转换为这种格式。

Though setting the culture in the web.config should do it, this should force it into that format.

更新

好了,上面的答案并不能真正解决问题,但如果你想改变的日期最初是如何显示的格式是很重要的。一个重要的注意的是, DisplayFormat 属性不被 TextBoxFor 帮手回升,但它是由 EditorFor 帮手。

Ok, so the above answer doesn't really solve the issue, though it is important if you do want to change the format of how the date is initially displayed. An important note is that the DisplayFormat attribute is not picked up by the TextBoxFor helper but it is by the EditorFor helper.

总之,就到了真正的解决方案。问题是解析日期时不占文化jQuery的验证。如果关闭了客户端验证的日期被解析蛮好的是意识到文化的服务器上。

Anyway, on to the real solution. The problem is the jQuery validation not accounting for the culture when parsing a date. If you turn off the client side validation the date is parsed just fine on the server that is aware of the culture.

解决方法是覆盖日期jQuery验证,并包括一个额外的jQuery插件的全球化。你可以找到在全球化插件 href=\"https://github.com/jquery/globalize\">。您也可以轻松地下载使用的NuGet包管理器以及插件。我刚打开包管理器,选择左侧的在线的标签,并输入全球化到搜索,这是第一个结果。一旦你安装我包括这两个文件:

The fix is to override the jQuery validation for date and include an additional jQuery globalization plugin. You can find the globalize plugin here. You can also easily download the plugin using the Nuget Package Manager as well. I just opened the package manager, selected the Online tab on the left and typed "globalize" into the search and it was the first result. Once you have it installed I included these two files:



  globalize.culture.en-AU.js

globalize.js
globalize.culture.en-AU.js

您可以包括他们直接使用脚本标记或将它们放在一个包,也许与其他jQuery验证文件。

You can either include them directly using a script tag or place them in a bundle, perhaps with the other jQuery validation files.

一旦你拥有了这些,你将需要添加下面的脚本来覆盖日期jQuery验证:

Once you have those you will need to add the following script to override the jQuery validation for date:

<script type="text/javascript">
    $(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("en-AU");
            // you can alternatively pass the culture to parseDate instead of
            // setting the culture above, like so:
            // parseDate(value, null, "en-AU")
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });
</script>

这就是它,这应该做的伎俩。我贷记本解决了这个答案:<一href=\"http://stackoverflow.com/questions/9779527/jquery-validation-and-mvc-3-how-to-change-date-format\">JQuery验证和MVC 3,如何更改日期格式我也想提供特定于您的问题更多一些的解释。

And that's it, that should do the trick. I credit this solution to this answer: JQuery Validation and MVC 3. How to change date format I just also wanted to provide some more explanation specific to your issue.

这篇关于MVC 4日期间的文化问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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