asp.net mvc3 data Validation (ii)--Customization and localization of error messages

Source: Internet
Author: User
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.

Related 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.