当前位置:  开发笔记 > 编程语言 > 正文

Bootstrap 3 Datepicker和DateTime验证错误

如何解决《Bootstrap3Datepicker和DateTime验证错误》经验,为你挑选了1个好方法。

我正在使用Bootstrap 3 Datepicker(http://eonasdan.github.io/bootstrap-datetimepicker/)为模型属性提供DateTime Picker:

模型:

[Table("Calls")]
public partial class Call
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required(ErrorMessage = "Campo obrigatório")]
    [Display(Name = "Data e Hora de início")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy HH:mm}")]
    public DateTime DateOfTheCall { get; set; }
}

视图:

@Html.LabelFor(model => model.DateOfTheCall, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.DateOfTheCall, new { htmlAttributes = new { @class = "form-control date" } }) @Html.ValidationMessageFor(model => model.DateOfTheCall, "", new { @class = "text-danger" })

我用日期格式设置了datepicker:

// initialise any date pickers
$('.date').datetimepicker({
    locale: 'pt',
    format: 'DD-MM-YYYY HH:mm'
});

我还在web.config文件中设置了文化:


但我总是收到错误消息:

The field Data e Hora de início must be a date.

Patrick.. 8

经过很长时间,我终于找到了一种基于jQuery Validation Globalize插件创建一个好解决方案的方法:

项目URL:https://github.com/johnnyreilly/jquery-validation-globalize

此扩展具有以下依赖项:

jQuery验证(本身依赖于jQuery)

全球化v1.x(本身取决于CDLR)

最终的代码

首先,包括库(尊重订单):
















我将模块模式用于脚本:

// Module Pattern
// More information: http://toddmotto.com/mastering-the-module-pattern/

var Layout = (function ($) {
    // Prevents certain actions from being taken and throws exceptions
    "use strict";

    // Private functions
    var _init = function () {

        // Use $.getJSON instead of $.get if your server is not configured to return the
        // right MIME type for .json files.
        $.when(
            $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"),
            $.getJSON("/Scripts/cldr/main/numbers.json"),
            $.getJSON("/Scripts/cldr/main/ca-gregorian.json"),
            $.getJSON("/Scripts/cldr/supplemental/timeData.json")
        ).then(function () {

            // Normalize $.get results, we only need the JSON, not the request statuses.
            return [].slice.apply( arguments, [ 0 ] ).map(function( result ) {
                return result[ 0 ];
            });

        }).then( Globalize.load ).then(function() {

            // Your local settings code goes here.
            Globalize.locale("pt-PT");
        });

        // initialise any date pickers (I had my own properties)
        $('.date').datetimepicker({
            locale: 'pt',
            format: 'DD-MM-YYYY HH:mm',
            sideBySide: true,
            showClose: true,
            defaultDate: new Date(Date.now()),
            toolbarPlacement: 'top',
            showTodayButton: true,
            showClear: true,
        });
    };

    return {

        // Public functions
        init: _init

    };

})(jQuery);

// Load when ready
$(document).ready(function () {
    Layout.init();
});

观点保持不变.



1> Patrick..:

经过很长时间,我终于找到了一种基于jQuery Validation Globalize插件创建一个好解决方案的方法:

项目URL:https://github.com/johnnyreilly/jquery-validation-globalize

此扩展具有以下依赖项:

jQuery验证(本身依赖于jQuery)

全球化v1.x(本身取决于CDLR)

最终的代码

首先,包括库(尊重订单):
















我将模块模式用于脚本:

// Module Pattern
// More information: http://toddmotto.com/mastering-the-module-pattern/

var Layout = (function ($) {
    // Prevents certain actions from being taken and throws exceptions
    "use strict";

    // Private functions
    var _init = function () {

        // Use $.getJSON instead of $.get if your server is not configured to return the
        // right MIME type for .json files.
        $.when(
            $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"),
            $.getJSON("/Scripts/cldr/main/numbers.json"),
            $.getJSON("/Scripts/cldr/main/ca-gregorian.json"),
            $.getJSON("/Scripts/cldr/supplemental/timeData.json")
        ).then(function () {

            // Normalize $.get results, we only need the JSON, not the request statuses.
            return [].slice.apply( arguments, [ 0 ] ).map(function( result ) {
                return result[ 0 ];
            });

        }).then( Globalize.load ).then(function() {

            // Your local settings code goes here.
            Globalize.locale("pt-PT");
        });

        // initialise any date pickers (I had my own properties)
        $('.date').datetimepicker({
            locale: 'pt',
            format: 'DD-MM-YYYY HH:mm',
            sideBySide: true,
            showClose: true,
            defaultDate: new Date(Date.now()),
            toolbarPlacement: 'top',
            showTodayButton: true,
            showClear: true,
        });
    };

    return {

        // Public functions
        init: _init

    };

})(jQuery);

// Load when ready
$(document).ready(function () {
    Layout.init();
});

观点保持不变.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有