Bootstrap 3 Datepicker和DateTime验证错误

时间:2015-12-08 02:28:21

标签: c# jquery twitter-bootstrap datetime datepicker

我使用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; }
}

查看:

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

我使用日期格式设置了datepicker:

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

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

<globalization culture="pt-PT"/>

但我总是收到错误消息:

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

2 个答案:

答案 0 :(得分:8)

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

此扩展程序具有以下依赖项:

  • jQuery验证(本身依赖于jQuery)
  • 全球化v1.x(本身取决于CDLR)

最终代码

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

<script src="~/Scripts/cldr.js"></script>
<script src="~/Scripts/cldr/event.js"></script>
<script src="~/Scripts/cldr/supplemental.js"></script>
<script src="~/Scripts/cldr/unresolved.js"></script>
<script src="~/Scripts/globalize.js"></script>
<script src="~/Scripts/globalize/number.js"></script>
<script src="~/Scripts/globalize/date.js"></script>
<script src="~/Scripts/moment.min.js"></script>
<script src="~/Scripts/moment-with-locales.min.js"></script>
<script src="~/Scripts/bootstrap/bootstrap.js"></script>
<script src="~/Scripts/respond/respond.js"></script>
<script src="~/Scripts/jquery/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery/jquery.validate.globalize.min.js"></script>
<script src="~/Scripts/bootstrap-datetimepicker.js"></script>

我将模块模式用于脚本:

// 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 :(得分:1)

我认为语言环境:错误地提到“pt”。请将其删除并进行测试。

请阅读以下内容:

enter image description here