Class CompareAttribute
Provides an attribute that compares two properties of a model.
[Obsolete("The recommended alternative is to use the System.ComponentModel.DataAnnotations.CompareAttribute type, which has the same functionality as this type.")]
[AttributeUsage(AttributeTargets.Property)]
public class CompareAttribute : ValidationAttribute, IClientValidatable
- Inheritance
-
CompareAttribute
- Implements
- Inherited Members
Constructors
CompareAttribute(string)
Initializes a new instance of the CompareAttribute class.
public CompareAttribute(string otherProperty)
Parameters
otherProperty
stringThe property to compare with the current property.
Properties
OtherProperty
Gets the property to compare with the current property.
public string OtherProperty { get; }
Property Value
- string
The property to compare with the current property.
OtherPropertyDisplayName
Gets the other properties display name.
public string OtherPropertyDisplayName { get; }
Property Value
- string
The other properties display name.
Methods
FormatErrorMessage(string)
Applies formatting to an error message based on the data field where the compare error occurred.
public override string FormatErrorMessage(string name)
Parameters
name
stringThe name of the field that caused the validation failure.
Returns
- string
The formatted error message.
FormatPropertyForClientValidation(string)
Formats the property for client validation by prepending an asterisk (*) and a dot.
public static string FormatPropertyForClientValidation(string property)
Parameters
property
stringThe property.
Returns
- string
The string "*." is prepended to the property.
GetClientValidationRules(ModelMetadata, ControllerContext)
Gets a list of compare-value client validation rules for the property using the specified model metadata and controller context.
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
Parameters
metadata
ModelMetadataThe model metadata.
context
ControllerContextThe controller context.
Returns
- IEnumerable<ModelClientValidationRule>
A list of compare-value client validation rules.
IsValid(object, ValidationContext)
Determines whether the specified object is equal to the compared object.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
Parameters
value
objectThe value of the object to compare.
validationContext
ValidationContextThe validation context.
Returns
- ValidationResult
null if the value of the compared property is equal to the value parameter; otherwise, a validation result that contains the error message that indicates that the comparison failed.