I have a model that has a datetime property and I want to make sure that in the view, the form can't be submitted unless that editor for has a value.
employee {
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Required] // <- this isn't doing anything for me???
public DateTime DateOfBirth {get;set;}
}
is there an annotation I can use for this or do I have to use javascript in the page?
or is there another solution?
Update -
When I clear out the date editor, I get the following in my editor box:
mm/dd/yyyy
when I submit this, does this count as null or what? Making the DateTime property nullable didn't fix my issue, theres no validation taking place when I submit a form that has mm/dd/yyyy for the date
Your problem is that DateTime
always has a value .
You'll need to make it a nullable DateTime
:
[Required]
public DateTime? DateOfBirth { get; set; }
Now your property will be null when no value is present and your Required
attribute will behave as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.