6

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?

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

Comments

6

Try making the DOB DateTime nullable like @Mayo states:

public DateTime? DOB { get; set; }

Comments

1

DateTime is of type struct. So, by default DateTime cannot be null. It has default value equal to '01/01/0001'. Solution to your problem is to use the nullable DateTime? type. If you want it to default to some value like '01/01/2014', then you can assign value like: DOB = new DateTime(2014, 01, 01);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.