2016-04-29 39 views
0

我在我的ViewModel以下字段:的DateTimePicker與ASP.NET MVC

[Display(Name = "Datum und Uhrzeit der Vorstellung")] 
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")] 
public DateTime DateAndTimeOfPerformance { get; set; } 

,並顯示它在一個局部視圖方式如下:

<div class="form-group"> 
    @Html.LabelFor(model => model.DateAndTimeOfPerformance, new { @class = "control-label" }) 
    <div class="input-group date datetimepicker"> 
     @Html.TextBoxFor(model => model.DateAndTimeOfPerformance, "{0:dd.MM.yyyy HH:mm}", new { @class = "form-control", @Value = Model?.DateAndTimeOfPerformance.ToString("dd.MM.yyyy HH:mm") }) 
     <span class="input-group-addon"> 
      <span class="glyphicon glyphicon-calendar"></span> 
     </span> 
    </div> 
    @Html.ValidationMessageFor(model => model.DateAndTimeOfPerformance, null, new { @class = "text-danger" }) 
</div> 

就像你看到的,我用引導的的DateTimePicker:

$('.datetimepicker').datetimepicker({ 
     format: "DD.MM.YYYY hh:mm" 
    }); 

但我總是得到驗證錯誤:

The field Datum und Uhrzeit der Vorstellung must be a date.

從我對Stackoverflow的研究中,我發現使用DateTime可能會非常棘手,但是我沒有找到與我的相匹配的示例來了解如何使其工作。

是否有任何方法來結合MVC驗證和Bootstrap的Datetimepicker?

感謝

+0

您的應用程序的文化和uiCulture的價值是什麼?你可以在web.config中設置它們。如果未設置,默認將使用en-EN,因此您的值無法解析爲dateTime。 –

+0

默認情況下,'jQuery.validator'驗證基於'MM/dd/yyyy'格式的日期,因此您需要重新配置驗證器。參考[這個答案](http://stackoverflow.com/questions/27285458/jquery-ui-date-picker-and-mvc-view-model-type-datetime/27286969#27286969)的一些選項 –

回答

0

這絕對是值的你DateAndTimeOfPerformance可變格式的問題。控件無法將其解析爲正確的日期時間。檢查格式。

另外,檢查文化。它可能會導致問題並將DateTime混淆成datetimepicker無法識別的格式。

使用帶有datetimepicker的控件並檢查正在使用的值。你可以從那裏去。