DataAnnotation [DataType (DataType.Date)] makes my field unnecessarily necessary.

I have the following field in my ViewModel:

[DataType(DataType.Date)] [Display(Name = "Preferred date)")] public DateTime EventDate { get; set; } 

which produces the following markup:

 <input class="input-validation-error form-control text-box single-line" data-val="true" data-val-date="The field Preferred date must be a date." data-val-required="The Preferred date field is required." id="EventDate" name="EventDate" type="date" value=""> 

If the user has not selected a value (that is, leave this field empty - value="" ), the returned data in the ViewModel will be "{1/1/0001 12:00:00 AM}" and ModelState.IsValid false .

I do not want this field to be required!

I tried replacing the markup with mine:

 <input class="form-control text-box single-line" id="EventDate" name="EventDate" type="date" value="1/1/1980"> 

However, the return value is still "{1/1/0001 12:00:00 AM}" and ModelState.IsValid is false .

How to disable mandatory verification in this field (only)? Thanks in advance.

+4
source share
1 answer

Thanks Murilo I will send the answer:

I changed the ViewModel field to:

 public DateTime EventDate { get; set; } 

to

 public Nullable<DateTime> EventDate { get; set; } 
+5
source

All Articles