In asp.net mvc can be used to inherit validationattribute in the way, customize the implementation of model two of the two property values comparison verification
The specific scenario is: To verify the size of the two property values
The code looks like this:
<summary>///Specifies the field must compare favourably with the named field, if objects to check AR e not of the same type///false would be return///</summary> public class Comparevaluesattribute:val Idationattribute {///<summary>///to compare to///</SUMMARY&G
T
public string Otherproperty {get; set;}
Public Comparevalues the Criteria {get; set;} <summary>///creates the attribute///</summary>///<param name= "Otherproper Ty ">the" other property to compare To</param> public Comparevaluesattribute (string otherproperty, Comparev Alues criteria) {if (Otherproperty = = null) throw new ArgumentNullException ("Otherpro
Perty ");
Otherproperty = Otherproperty;
criteria = criteria; }///<summary>///Determines whether the specified value of the object is valid. For this to is the case, the objects must is the same type///and satisfy the criteria. Null values would return false in the all cases except when both///objects null. The objects would need to implement IComparable for the Greaterthan,lessthan,greatthanorequalto and Lessthanorequalto Insta
NCEs///</summary>///<param name= "value" >the value of the object to validate</param> <param name= "Validationcontext" >the validation context</param>///<returns>a Valida tion result if the object is invalid, null if the object is valid</returns> protected override Validationres
Ult IsValid (object value, Validationcontext validationcontext) {//The other property
var property = ValidationContext.ObjectType.GetProperty (Otherproperty);
Check it is not null if (property = = null) return to New Validationresult (String.Format ("Unknown property: {0}.", Otherproperty)
); Check types var membername = ValidationContext.ObjectType.GetProperties (). Where (P => p.getcustomattributes (FALSE). Oftype<displayattribute> (). Any (a => a.name = = validationcontext.displayname)). Select (P => p.name).
FirstOrDefault ();
if (membername = = null) {membername = Validationcontext.displayname; } if (ValidationContext.ObjectType.GetProperty (membername). PropertyType!= property. PropertyType) return to New Validationresult (String.Format ("the types of {0} and {1} must be the same.", Memb
Ername, Otherproperty)); Get the ' other value var ' = property.
GetValue (validationcontext.objectinstance, NULL);
equals to comparison, if (Criteria = = Comparevalues.equalto) { if (Object.Equals (value, other)) is return null; else if (Criteria = = Comparevalues.notequalto) {if (!
Object.Equals (value, other)) return null; else {//check that both objects are icomparables if (! Value is IComparable | | !
(is IComparable)) return new Validationresult (String.Format ("{0} and {1} must both implement IComparable", Validationcontext.displayname,
Otherproperty));
Compare the objects var = Comparer.Default.Compare (value, other); Switch (Criteria) {case CompareValues.GreaterThan:if (Result
> 0) return null;
Break Case CompareValues.LessThan:if (resUlt < 0) return null;
Break Case CompareValues.GreatThanOrEqualTo:if (Result >= 0) return null
;
Break
Case CompareValues.LessThanOrEqualTo:if (Result <= 0) return null;
Break }//Got this far must mean the items don ' t meet the criteria return
New Validationresult (errormessage); }///<summary>///indicates a comparison criteria used by the Comparevalues attribute///& Lt;/summary> public enum Comparevalues {equalto, Notequalto, GreaterThan, less Than, Greatthanorequalto, Lessthanorequalto}
Add this comparevaluesattribute tag directly to the specified attribute at the time of application
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/