Search code examples
c#asp.net-mvcdatetimedata-annotations

ASP.NET MVC Data Annotations DateTime Default Value


In my ViewModel I have the following attribute:

[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; }

In my View I have the following:

<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>

Before submitting the form the default value for DOB is 1/01/0001, how do I stop this value from being auto-populated, I simply want an empty field when people visit this form?


Solution

  • I believe you will have to use the nullable DateTime? type. DateTime cannot be null thus it will always have a value.