如何验证日期时间

问题描述 投票:0回答:1

我在验证日期时遇到了麻烦,我在班上有一个属性,我希望这个属性也能收到日期和时间的值。例如:dd / mm / yyyy hh:mm:ss。

这是我在模型中的属性:

[Display(Name = "Begin Date:")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy hh:mm:ss}")]
[DataType(DataType.DateTime, ErrorMessage = "Invalid Format")]
public DateTime BeginDate { get; set; }

这就是我实现它的方式:

@Html.EditorFor(model => model.BeginDate, new { htmlAttributes = new { @class = "form-control", @data_type = "datetime" } })
                                @Html.ValidationMessageFor(model => model.BeginDate, "", new { @class = "text-danger" })

当我发短信11/09/2018它没有抱怨任何东西,但发短信时11/09/2018 11:51:00它会引发The field Begin Date must be a date

c# jquery asp.net-mvc jquery-ui razor
1个回答
0
投票

我想你应该写:

@Html.EditorFor(model => model.BeginDate, new { htmlAttributes = new { @class = "form-control", @data_type = "datetime" } })
@Html.ValidationMessageFor(model => model.BeginDate, "", new { @class = "text-danger" })
© www.soinside.com 2019 - 2024. All rights reserved.