I'm really lost trying to create custom generic validation messages for my MVC3 application.
I read many tutorials and the biggest part of them suggest one of these options:
Create an App_GlobalResources folder and inside that put my custom resource, them I change global.asax, passing the string name of my resource to
DefaultModelBinder.ResourceClassKey
property. Change the resource's build action to "Embeded Resource".Right click the project, then go properties, click "Resources" tab and create your custom resource. Set it's access modifier to "Public".
The first one didn't work to me, I have the following keys in my resource: DefaultModelBinder_ValueRequired, InvalidPropertyValue, PropertyValueInvalid, PropertyValueRequired and none of them was used when my I tried to submit and form with empty value in a required attribute of my model. I've put this code at global.asax Application_Start method:
DefaultModelBinder.ResourceClassKey = "My_Resource_Name";
With the second method, I've created an resource with the same keys as the first one. None of them was used by default when a property is invalid or empty (I've changed the ResourceClassKey in global.asax too, but without success). But when I added some parameters to data annotation in my model:
[Required(ErrorMessageResourceType = typeof(MyResourceFile), ErrorMessageResourceName = "MyCustomKey")]
When the attribute of that data annotation is empty, my message defined with "MyCustomKey" is used!
But I really don't want to manually set this to all my attributes, I want to replace the default error messages like: "The {0} field is required."