First, the custom error message in the previous article in the verification, in the interface prompts the information is the system itself, some read more blunt. Like what:
;
If your environment is in English, then your message is mixed with Chinese and English. In this case, you need to use custom error messages. In fact, it is very simple, is in the model of the verification features added [ErrorMessage]. For example: Model:
[Required (errormessage = "User name cannot be empty.) ")]
[Display (name =" user name ")]
[Remote (" Checkusername "," account "]] is public
string UserName {get; set;}
Validation results:
ErrorMessage allows developers to use the {0} placeholder to display the display name of the field (that is, [Display (name = username)]), and if there is no display attribute, the property name is displayed. such as: Model:
[Required (errormessage = ' {0} cannot be empty.) ")]
[Display (name =" user name ")]
[Remote (" Checkusername "," account "]] is public
string UserName {get; set;}
Validation results:
If the:
[Required (errormessage = ' {0} cannot be empty.) "]]
//[display (name =" username ")]
[Remote (" Checkusername "," account ")] is public
string UserName {get; set;}
The result of the validation is:
If there are other parameters in the validated attribute, then errormessage can display other parameters directly, such as Model:
[Required]
[Stringlength errormessage = "Please enter {2} to {1} bit {0}. ", Minimumlength = 6)]
[DataType (Datatype.password)]
[Display (Name = ' password ')]] public
string Password {get; Set }
Validation results:
Ii. Localization of error information if you are doing projects that you want to distribute to different countries, then you need to localize them. For error messages, there are also the following methods: 1. Add two resource files to the project Errormessages.resx and Errormessages.en-us.resx:
2, in the two resource files are added to the name of Usernamerequire resources, the value is the Chinese and English under the hint information:
3, in the Web.config <system.web> node to join the <globalization uiculture= "Auto"/>, with the browser settings to replace the resource file. 4, in model also like the following code:
[Required (Errormessageresourcetype = typeof (Errormessages), errormessageresourcename = "Usernamerequire")]
[Display (name = "user name")]
public string UserName {get; set;}
5, the operation result: Chinese environment:
In English environment:
OK, mvc3 the error message customization and localization is simpler, for the time being summed up so much, the next one is going to talk about customizing data validation.