ASP .NET MVC的DateTime全球化。 "外地日期必须是日期和QUOT。 [英] ASP .NET MVC DateTime globalization. "The field Date must be a date."

查看:219
本文介绍了ASP .NET MVC的DateTime全球化。 "外地日期必须是日期和QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的MVC应用程序测试全球化/本地化。
我已经Resources.resx文件,并Resources.es.resx(西班牙语)文件在那里我有我想要翻译的字符串。

所有的字符串进行翻译不错,但与DateTime格式
我遇到的困难。

在局部视图我使用我用XD-软的DateTimePicker我的日期字段是这样的:

剃刀语法:

 < D​​IV CLASS =表单组>
        @ Html.LabelFor(型号=> model.Date,htmlAttributes:新{@class =控制标签COL-MD-3})
        < D​​IV CLASS =COL-MD-10>
            @ Html.EditorFor(型号=> model.Date,Resources.FormatSave,新{htmlAttributes = {新@class =的形式控制输入-SM datetimepicker1}})
            @ Html.ValidationMessageFor(型号=> model.Date,新{@class =TEXT-危险})
        < / DIV>
    < / DIV>

和dateTimePicker的剧本是这样的:

 <脚本类型=文/ JavaScript的>    jQuery的('。datetimepicker1')。的DateTimePicker({
        格式:@ Resources.Format',//当地方d.m.Y,当西班牙D / M / Y
        主题:'暗',
        郎咸平:@ Resources.Language',
        closeOnDateSelect:真实,
    });< / SCRIPT>

当我用西班牙语格式D / M / Y的好吧,当我使用d.m.Y我得到验证消息字段的日期必须是日期。

我的模型是这样的:

  [显示(名称=日期,的ResourceType = typeof运算(Resources.Resources))]
[必填(ErrorMessageResourceType = typeof运算(Resources.Resources)
          ErrorMessageResourceName =DateRequired)]
[数据类型(DataType.DateTime)
公众的DateTime日期{搞定;组; }


解决方案

刚刚尝试这一点。我发现这个解决方案时,我的DD / MM / YYYY格式不被识别为日期格式。

首先创建一个名字一个新的Java脚本文件 jquery.validate.date.js 与下面code在它

  $(函数(){
    $ .validator.methods.date =功能(价值元素){
        如果($ .browser.webkit){
            变种D =新的日期();
            返回this.optional(元)|| !/无效|为NaN / .TEST(新日期(d.toLocaleDateString(值)));
        }
        其他{
            返回this.optional(元)|| !/无效|为NaN / .TEST(新日期(值));
        }
    };
});

它覆盖 jquery.validate.js

日期验证函数

然后调用脚本刚过 jquery.validate.js 像下面

 <脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/脚本/ jquery.val.js)/><脚本类型=文/ JavaScript的SRC =@ Url.Content(〜/脚本/ jquery.val.date.js)/>

I have made a simple mvc app to test globalization/localization. I have made Resources.resx file and Resources.es.resx (spanish) file where I have strings that I want translated.

All strings are translated fine, but with DateTime format I am experiencing difficulties.

In the partial view I am using I use xd-soft datetimepicker for my date field like this:

razor syntax:

<div class="form-group">
        @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-3" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date, Resources.FormatSave, new { htmlAttributes = new { @class = "form-control input-sm datetimepicker1" } })
            @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
        </div>
    </div>

and the script for datetimepicker looks like this:

<script type="text/javascript">

    jQuery('.datetimepicker1').datetimepicker({
        format: '@Resources.Format', //when local d.m.Y, when spanish d/m/Y
        theme: 'dark',
        lang: '@Resources.Language',
        closeOnDateSelect: true,
    });

</script>

When i use spanish format d/m/Y its alright and when i use d.m.Y i get validation message "The field Date must be a date.".

My model looks like this:

[Display(Name = "Date", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
          ErrorMessageResourceName = "DateRequired")]
[DataType(DataType.DateTime)]
public DateTime Date { get; set; }

解决方案

Just try this. I found this solution when my dd/MM/yyyy format is not recognized as date format.

First create a new java script file with name jquery.validate.date.js with the below code in it

$(function () {
    $.validator.methods.date = function (value, element) {
        if ($.browser.webkit) {
            var d = new Date();
            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
        }
        else {
            return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
        }
    };
});

It overwrite the date validation function in jquery.validate.js

Then call the script just after jquery.validate.js like below

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.val.js")"/>

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.val.date.js")"/>

这篇关于ASP .NET MVC的DateTime全球化。 &QUOT;外地日期必须是日期和QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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