Original: ASP. MVC3 data Validation (ii)--Customization of error messages and their localization
I. Custom error messages in the previous article to verify that the information prompted on the interface is the system comes with, some read more blunt. Like what:
;
If your environment is in English, then your cue message is mixed in Chinese and English. In this case, you need to use the custom error message. In fact, it is simple to add [errormessage] to the model validation feature. For example: Model:
" the user name cannot be empty! (")] [ user name ]] [Remote ( " Checkusername ","account")] publicstring Get set; }
Validation results:
ErrorMessage allows developers to use the {0} placeholder to display the field's display name (that is,[Display(name = "user name")]), if there is no display attribute, the property name is displayed. such as: Model:
" {0} cannot be empty! (")] [ user name ]] [Remote ( " Checkusername ","account")] publicstring Get set; }
Validation results:
If this is:
" {0} cannot be empty! ")] //[Display (name =" username ")] [Remote (" checkusername","account")] publicstringgetset; }
The result of the validation is:
If there are additional parameters in the validated attribute, then errormessage can display additional parameters directly with a placeholder, such as model:
[Required] [Stringlength ( Please enter {2} to {1} bits of {0}. "6)] [DataType (Datatype.password)] " Password " )] publicstringgetset; }
Validation results:
ii. Localization of error messagesIf you are doing projects that you want to distribute to different countries, then you need to localize them. For error messages, there are also features such as the following: 1. Add two resource files to the project Errormessages.resx and Errormessages.en-us.resx:
2, in two resource files are added to the name Usernamerequire resources, the values are in Chinese and English, respectively, the prompt information:
3. In the Web. config<system.web>Node Join<globalization uiCulture="auto"/>, replace the resource file with the browser's settings. 4, in the model also as the following code:
typeof " Usernamerequire " )] [ user name ]] publicstring getset; }
5, the operation result: Chinese environment:
English environment:
OK, mvc3 error message customization and localization is relatively simple, for the time being summed up so much, the next one is about custom data validation.