【问题标题】:jQuery time picker and MVC 4 validation - the field must be a datejQuery 时间选择器和 MVC 4 验证 - 字段必须是日期
【发布时间】:2013-07-02 20:09:14
【问题描述】:

我正在使用这个 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 字段必须是日期。

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

【问题讨论】:

  • DisplayFormat 是您在 ScheduleTime 属性上拥有的唯一数据注释属性吗?
  • @ataravati 我也有 [Display(Name = "Scheduled time")]

标签: jquery asp.net-mvc validation timepicker


【解决方案1】:

像这样在模型中使用 DataType 属性:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多