ASP. MVC3 data Validation (ii)--Customization of error messages and their localization

Source: Internet
Author: User

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.  

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.