Asp.net mvc3 data verification (2) -- error information customization and Localization

Source: Internet
Author: User
   I. custom error messagesFor the verification in the previous article, the information prompted on the interface is all built-in and some are relatively hard to read. For example:

;

If your environment is English, your prompt information is mixed in Chinese and English. In this case, you need to use custom error messages. In fact, it is very easy to add [errormessage] to the model verification feature. For example: Model:

[Required (errormessage = "the user name cannot be blank! ")] [Display (name =" username ")] [Remote (" checkusername "," account ")] Public String username {Get; set ;}

 

Verification Result:

Errormessage allows developers to use the {0} placeholder to display the field display name ([display (name = "username")]). If the display feature is not available, the property name is displayed. Example: Model:

[Required (errormessage = "{0} cannot be blank! ")] [Display (name =" username ")] [Remote (" checkusername "," account ")] Public String username {Get; set ;}

 

Verification Result:

If:

[Required (errormessage = "{0} cannot be blank! ")] // [Display (name =" username ")] [Remote (" checkusername "," account ")] Public String username {Get; set ;}

 

The verification result is:

If there are other parameters in the verification feature, the errormessage can be used as a placeholder to directly display other parameters, such as model:

[Required] [stringlength (100, errormessage = "Please enter {2} to {1} {0 }. ", Minimumlength = 6)] [datatype (datatype. Password)] [display (name =" password ")] Public String password {Get; set ;}

 

Verification Result:

Ii. Localization of error messagesIf your project is to be distributed to different countries, You need to implement localization. For the error message, there is also the function, the method is as follows: 1, add two resource files errormessages. resx and errormessages. en-US.resx in the project:

2. Add a resource named usernamerequire to both resource files. The values are prompts in Chinese and English:

3. Add <globalization uiculture = "Auto"/> to the <system. Web> node of Web. config and change the resource file with the browser settings. 4. The following code is also used in the model:

[Required (errormessageresourcetype = typeof (errormessages), errormessageresourcename = "usernamerequire")] [display (name = "username")] Public String username {Get; set ;}

 

5. Running result: in a Chinese environment:

In an English environment:

Now, the customization and localization of error messages in mvc3 are relatively simple. To sum up so much, we will talk about custom data verification in the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.