我在验证日期时遇到了麻烦,我在班上有一个属性,我希望这个属性也能收到日期和时间的值。例如: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
。
我想你应该写:
@Html.EditorFor(model => model.BeginDate, new { htmlAttributes = new { @class = "form-control", @data_type = "datetime" } })
@Html.ValidationMessageFor(model => model.BeginDate, "", new { @class = "text-danger" })