2015-12-08 52 views
19

我使用的是引导3日期选择器(http://eonasdan.github.io/bootstrap-datetimepicker/)提出一个日期时间选择器的模型属性:引导3日期选择器和日期时间验证错误

型号:

[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> 

我设定的日期格式的日期选择器:

// 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. 
+1

你可以通过javascript验证你的datetimepicker,创建一个函数来检查值。 –

+1

我认为你的@ Html.EditorFor是以type ='textbox'的文本框生成的,但它应该是type ='date'。这可能可以帮助你 – Nimmi

+1

@Nimmi嗨,我该如何改变EditorFor的类型? – Patrick

回答

8

了很多小时后,我终于找到了一种方法来创建一个良好的解决方案,基于jQuery的验证Globalize的插件:

此扩展有以下依赖关系:

  • jQuery验证(其本身取决于jQuery的)
  • 全球化1.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

我觉得语言环境: “PT” 被错误地提及。请删除它并测试。

请阅读以下:

enter image description here

+1

嗨,感谢您的帮助,但我尝试了您的解决方案,但无法使其运行。你可以查看我的看法,看看你是否同意。 – Patrick