gpt4 book ai didi

c# - 在 MVC 日期时间?不工作

转载 作者:行者123 更新时间:2023-11-30 22:18:40 24 4
gpt4 key购买 nike

我正在开发 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")

Shared/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 中?

最佳答案

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

关于c# - 在 MVC 日期时间?不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com