尽管文化en-GB

时间:2016-09-28 14:56:59

标签: c# asp.net-mvc-5 locale

我在MVC中有一系列的Web表单。在最近对网站进行一些更新之后,DateTime字段验证假设日期是美国格式,因此大于12的天数未通过验证,尽管事实上我已将编辑日期格式,验证代码和文化指定为GB。尝试了各种浏览器,所有浏览器都设置为英语(英国),并且验证在Firefox,Chrome和Edge中失败,并且仅适用于IE。尝试过这里发现的各种其他建议,包括ModelBinding,但似乎没有任何效果。

在课堂上:

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

在web.config中:

<globalization culture="en-GB" uiCulture="en-GB" requestEncoding="utf-8" responseEncoding="utf-8" enableClientBasedCulture="false" />

在global.asax中:

CultureInfo newCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
newCulture.DateTimeFormat.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-GB");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");

在jquery.validate.js中:

date: function (value, element) {
        $.culture = Globalize.culture("en-GB");
        var date = Globalize.parseDate(value, "dd/MM/yyyy", "en-GB");
        return this.optional(element) || 
                       !/Invalid|NaN/.test(new Date(date).toString());
    }

还有其他建议吗?

1 个答案:

答案 0 :(得分:1)

我让它与MVC 5一起正常工作。需要进行两项更改:

  1. 要添加模型活页夹,因为默认活页夹不会考虑CurrentUICulture。我做了这个,删除客户端验证只是为了测试它。它默认情况下不会使用URL中的日期,但我认为如果我这样做,我会想要沿着yyyy-mm-dd行格式化URL。
  2. 更改客户端验证以考虑英国日期。我不确定你的jquery.validate.js代码是否有效。我也遇到了一些麻烦,所以最后我安装了nuget包jQuery.Validation.AdditionalMethods并制作了使用它的代码。
  3. http://www.ablogaboutcoding.com/2017/08/12/mvc-uk-date-issues/我自己的博客文章进一步解释。

相关问题