我正在使用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(); });
观点保持不变.
经过很长时间,我终于找到了一种基于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(); });
观点保持不变.