This post in the blog garden is already comprehensive, but I think it is not elegant enough. It seems like a mess, and this old man's post abroad is neat and tidy, A simple record is as follows:
General international Processing Methods
Public Class User {[required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_id ", Nameresourcetype = Typeof (Modeltranslations)] Public Int Id { Get ;Set ;} [Required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [stringlength (40, errormessageresourcename =" Validation_stringlength ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_firstname ", Nameresourcetype = Typeof (Modeltranslations)] Public String Firstname {Get ; Set ;} [Required (errormessageresourcename =" Validation_required ", Errormessageresourcetype = Typeof (Modeltranslations)] [stringlength (40, errormessageresourcename =" Validation_stringlength ", Errormessageresourcetype = Typeof (Modeltranslations)] [localizeddisplaynameattribute (" User_lastname ", Nameresourcetype = Typeof (Modeltranslations)] Public String Lastname { Get ; Set ;}}
We can see that it is confusing.
Public Class User {[required] [localizeddisplaynameattribute (" User_id ")] Public Int Id { Get ; Set ;} [Required] [stringlength (40)] [localizeddisplaynameattribute (" User_firstname ")] Public String Firstname { Get ; Set ;} [Required] [stringlength (40)] [localizeddisplaynameattribute (" User_lastname ")] Public String Lastname { Get ; Set ;}}
Is it much more neat, but the cost of cleanliness is to writeCodeOf
Public Class Requiredattriations: system. componentmodel. dataannotations. requiredattriations {Private String _ Displayname; Public Requiredattribute () {errormessageresourcename =" Validation_required ";} Protected Override Validationresult isvalid ( Object Value , Validationcontext) {_ displayname = validationcontext. displayname; Return Base . Isvalid ( Value , Validationcontext );} Public Override String Formaterrormessage ( String Name) {var MSG = Your ageservice. instance. Translate (errormessageresourcename ); Return String . Format (MSG, _ displayname );}}
This is the implementation code for verifying properties.
Ublic Class Localizeddisplaynameattribute: displaynameattribute { Private Propertyinfo _ nameproperty; Private Type _ resourcetype;Public Localizeddisplaynameattribute ( String Classname, String Propertyname ): Base (Classname + (propertyname = Null ? " _ Class ":(" _ "+ Propertyname ))){} Public Override String Displayname { Get { Return Languageservice. instance. Translate ( Base . Displayname )?? " ** "+ Base . Displayname +" ** ";}}}
This is the implementation code for implementing the displayname attribute.
Public Class Languageservice { Private Static Required ageservice _ instance = New Languageservice (); Private List <ResourceManager> _ resourcemanagers = New List <ResourceManager> (); Private Languageservice (){} Public Static Languageservice instance { Get { Return _ Instance ;}} Public Void Add (ResourceManager MGR) {_ resourcemanagers. Add (MGR );} Public String Translate ( String Key ){ Foreach (VAR itemIn _ Resourcemanagers) {VaR Value = Item. getstring (key ); If ( Value ! = Null ) Return Value ;} Return Null ;}}
This is the code for managing resources dependent on the above class.
However, the above solution is still problematic for client verification. The following is a simple implementation:
Public Class Requiredattributeadapter: dataannotationsmodelvalidator <requiredattribute> { Public Requiredattributeadapter (modelmetadata metadata, controllercontext context, requiredattribute attribute ): Base (Metadata, context, attribute ){} Public Static Void Selfregister () {dataannotationsmodelvalidatorprovider. registeradapter ( Typeof (Requiredattribute ), Typeof (Requiredattributeadapter ));} Public Override Ienumerable <modelclientvalidationrule> getclientvalidationrules (){ Return New [] { New Modelclientvalidationrequiredrule (errormessage )};}}
Add a sentence to global. asax.
Requiredattributeadapter. selfregister ();
Technorati label: ASP. net mvc verification Internationalization
.