在我的ViewModel中,我有以下属性:
[Required]
[DataType(DataType.Date, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
[Display(Name = "Date of Birth")]
public DateTime DOB { get; set; }
在我的 View 中,我有以下内容:
<div class="editor-label">
@Html.LabelFor(model => model.DOB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
</div>
在提交表单之前,DOB
的默认值为 1/01/0001
,如何阻止自动填充该值,我只想一个空的当人们访问此表单时字段?
最佳答案
我相信您将不得不使用可为空的 DateTime?类型。 DateTime 不能为 null,因此它始终有一个值。
关于c# - ASP.NET MVC 数据注释日期时间默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351969/