I have properties declared in my view model like:
[Required(ErrorMessage = "The Date field is required for Start.")]
[Display(Name = "Start")]
public DateTime DateStart { get; set; }
However, I am still getting a default The Start field is required error message. I assume this is because a non-nullable DateTime is implicitly required, and the Required attribute is ignored. Is there a way to customise my error message for these specific properties, besides making them nullable?
You right, your problem is that your property is not nullable. For not nullable properties attribute
Requiredis meaningless. When there is no StartDate value, validation is not go to yourRequiredattribute and fails on previous step. If you want to get yourErrorMessageyou shoulduse:
You cannot customize ErrorMessage for nonullable types that get null on modelbinding, cause it is hardcoded deep in MVC framework.