jQuery时间选择器和MVC 4验证 - 该字段必须是日期

时间:2013-07-02 20:09:15

标签: jquery asp.net-mvc validation timepicker

我正在使用此jQuery插件来选择时间:http://jonthornton.github.io/jquery-timepicker/

我这样做我的时间戳:

$('#scheduletime').timepicker({ 'scrollDefaultNow': true, 'timeFormat': 'h:i A' });

导致时间以下列格式显示:

08:00 AM

我正在使用MVC客户端验证,我的模型如下所示:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
public DateTime? ScheduleTime { get; set; }

我的问题是我收到客户端验证错误:

  

字段ScheduleTime必须是日期。

出了什么问题?字符串验证格式似乎是正确的,但它不起作用。

1 个答案:

答案 0 :(得分:3)

在模型中使用DataType属性,如下所示:

[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[Display(Name = "Schedule Time")]
public DateTime? ScheduleTime { get; set; }