2015-12-08 50 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