ASP. net mvc error message Localization

Source: Internet
Author: User

Many friends asked how to verify messages by localize, instead of modifying the attribute values to convert them into hard-coded.

It is easy to do now. The examples involved below can run properly in ASP. net mvc 1.0 (ASP. net mvc 2.0 has not been tried ).

 

Let's start with the productviewmodel below:

Public ClassProductviewmodel {

[Price (minprice = 1.99)]

Public DoublePrice {Get; set ;}

 

[Required]

Public StringTitle {Get; set ;}

}

If we localize the error information of the above two attributes, we need to add resources to our project. Right-click the project, select properties, and click resources. The following message is displayed:

"This project does not contain the default resource file. Click here to create it. "

Follow the prompts. After the creation, you will see the resource file editor:

Change access modifier to public (internal by default)

Enter content to the resource file (the following uses Spanish as an example to distinguish English ):

ASP. NET creates a class named after resources. This class file contains the priceisnotright and required attributes. You can see from the property file of the project:

The next step is to modifyCodeTo make the error information added to the resource file take effect:

Code

Public   Class Productviewmodel
{
[Required (errormessageresourcetype =   Typeof (Resources), errormessageresourcename =   " Required " )]
Public   String Title { Get ; Set ;}

[Price (minprice =   3.99 , Errormessageresourcetype =   Typeof (Resources), errormessageresourcename =   " Priceisnotright " )]
Public   Double Price { Get ; Set ;}

}

 

As errormessageresourcetype, I only use the namespace created by the system:

 

Customvalidationattributeweb. properties. Resources.

Now, for example, when we submit invalid information, the error information will be read from the resource file and displayed to the user. Note that the Spanish language is displayed next to it, of course, you can try it in Chinese in the resource file:

 

 

Http://www.zivsoft.com/show.asp? Id = 55

 

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.