如何验证DateTime

时间:2018-09-11 16:15:15

标签: c# jquery asp.net-mvc jquery-ui razor

验证日期时间时遇到麻烦,我的课程中有一个属性,我也希望该属性也接收日期和时间的值。例如: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

1 个答案:

答案 0 :(得分:0)

我认为您应该写:

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