I have created a model in my asp.net MVC 3 website and have a property named DateOpened:
[Column("Date Opened")]
[Display(Name = "Date Opened:")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateOpened { get; set; }
I didn't apply [Required] data annotation to it, but when I try to save the form, It says required field. In database it is null.
Please suggest solution.
That's normal. DateTime is a value type meaning that it will always require a value. The model metadata provider in ASP.NET MVC automatically adds the required attribute to non-nullable data types. You could use a nullable DateTime:
[Column("Date Opened")]
[Display(Name = "Date Opened:")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? DateOpened { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With