Let's have a model
public class Model
{
public int Number { get; set; }
public DateTime Date { get; set; }
}
I observed following behavior. Number property has value 0 and Date has 1.1 0001 00:00:00 when nothing is submitted an the value of ModelState.IsValid is true.
The DataAnnotationsModelValidatorProvider class and GetValidators method has this pieco of code
// Add an implied [Required] attribute for any non-nullable value type,
// unless they've configured us not to do that.
if (AddImplicitRequiredAttributeForValueTypes &&
metadata.IsRequired &&
!attributes.Any(a => a is RequiredAttribute)) {
attributes = attributes.Concat(new[] { new RequiredAttribute() });
}
If I understand this right then the Number property and DateTime property should get set a RequiredAttribute and validation routine should set the model invalid and generate appropriate error messages.
So my question is why ins't the model invalid ?
I'm using ASP.NET MVC 3