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