ASP.net MVC Data Annotations DateTime Default Value
In my view model 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 being auto populated, i simply want an e开发者_运维百科mpty field when people visit this form?
I believe you will have to use the nullable DateTime? type. DateTime cannot be null thus it will always have a value.
Try making the DOB DateTime nullable like @Mayo states:
public DateTime? DOB { get; set; }
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);
精彩评论