Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net MVC3 DefaultModelBinder error messages keys

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 ?

like image 442
Johnny5 Avatar asked Feb 23 '23 03:02

Johnny5


1 Answers

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.

like image 147
csharpdev Avatar answered Mar 07 '23 14:03

csharpdev