4

There's a lot of question (and answer) here on stackoverflow and other site about translating the default error messages provided by the DefaultModelBinder, such as this one or this one.

All answers to these questions basically proprose to create a ressource (resx) file under App_GlobalResources and put a message for "PropertyValueInvalid".

That's fine, it does works, but there is other message also (ex. "The value xxx must be a number") and the big question is : where in hell can I have a list of the messages key used for model binding validation, with a description of the context they are used for so I can translate each of them ?

1 Answer 1

8

When DefaultModelBinder.ResourceClassKey = "ResourceFileName"; is set in Application_Start method the DefaultModelBinder class uses other resource keys than defined in the resource file used by Asp.Net MVC.

While disassembling DefaultModelBinder there is a method GetUserResourceString that reads from the custom resource file. This functions is only called twice with these messages keys:

  • PropertyValueRequired (A value is required.)
  • PropertyValueInvalid (The value '{0}' is not valid for {1}.)

These are the only messages that could be set for DefaultModelBinder in MVC3.

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't seem to work (MVC4). I added a ErrorMessages.resx in App_GlobalResources with my custom messsages, then set DefaultModelBinder.ResourceClassKey = "ErrorMessages"; ClientDataTypeModelValidatorProvider.ResourceClassKey = "ErrorMessages"; in Global.asax Application_Start... and still shows standard messages. Keys I added: FieldMustBeDate, FieldMustBeNumeric (these work), PropertyValueInvalid, PropertyValueRequired (these two don't work)
I am using MVC4 and it works for me. Did you manually create a folder and name it App_Globalresources or did you right click the project and add a ASP.NET Folder? Also, I did not add this: ClientDataTypeModelValidatorProvider.ResourceClassKey = "ErrorMessages"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.