Handling required fields in ASP.NET MVC when using Entity Framework
MVC is validating your model based on the type being non nullible, as you have discovered. This is adding errors to ModelState before your custom validation runs.
I had this before, and got round it by looping through Modelstate at the start of an action and removing everything, then doing my custom validation (bad!!)
Then found even if you are not using data annotations as your main form of validation, you can customise the message that is thrown by adding [Required to the non nullible type in the buddy class, and specify the message.
It's something like this:
[MetadataType(typeof(YourClassMetadata))]
public partial class YourClass
{
//buddyclass to entity class
class YourClassMetadata
{
[Required(ErrorMessage="Your custom overriding error message")]
public int NonNullablePropertyThatIsGivingYouProblems {get;set;}
}
}
I've started to look at fluent validation (http://fluentvalidation.codeplex.com) for mvc, and they seem to turn off the problem in global.asax, in on application_start() by adding the line
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
but I may be wrong about that.