2

我正在开发一个 ASP.Net MVC 4 应用程序。在 UI 中,我有一个字段使用时间选择器来选择开始和结束时间,格式为HH:mm. 但此刻我选择一个值 07:00 验证消息说The field Start Time must be a date.我正在使用 jQuery 验证插件:

class booking
{
DateTime? StartTime{get;set;}
}

看法

 @model booking
 StartTime:  @Html.EditorFor(m => m.StartTime, "TimePicker")

共享/EditorTemplates/TimePicker.cshtml

@model DateTime?
@{
String modelValue = "";
var dateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

if (Model.HasValue)
{
    if (Model.Value != DateTime.MinValue)
    {
        modelValue = Model.Value.ToString("HH:mm", dateFormat);
    }
}
}
@Html.TextBox("", modelValue)

呈现的 HTML

 <input type="text" value="" name="StartTime" id="StartTime" 
 data-val-date="The field Start time must be a date." data-val="true" 
 class="hasTimepicker input-validation-error" />

问题是什么?为什么 mvc 使用此编辑器模板触发验证?它在 mvc 3 中工作。为什么不在 mvc4 中?

4

1 回答 1

3

不是绑定到 DateTime,而是绑定到 TimeSpan。如果没有日期组件,DateTime 就不能存在。

于 2013-04-11T18:07:04.007 回答