Table of Contents

Class CompareAttribute

Namespace
System.Web.Mvc
Assembly
System.Web.Mvc.dll

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 string

The 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 string

The 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 string

The 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 ModelMetadata

The model metadata.

context ControllerContext

The 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 object

The value of the object to compare.

validationContext ValidationContext

The 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.