ASP.net MVC数据注释DateTime默认值

Ada*_*ien 6 asp.net-mvc data-annotations

在我的视图模型中,我有以下属性:

 [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; }
Run Code Online (Sandbox Code Playgroud)

在我看来,我有以下几点:

<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>
Run Code Online (Sandbox Code Playgroud)

在提交表单之前,DOB的默认值是1/01/0001如何停止自动填充此值,我只想在人们访问此表单时使用空字段?

May*_*ayo 7

我相信你将不得不使用可以为空的DateTime?类型.DateTime不能为null,因此它始终具有值.


amu*_*rra 6

尝试使DOB DateTime像@Mayo状态一样可以为空:

public DateTime? DOB { get; set; }
Run Code Online (Sandbox Code Playgroud)