Yesterday I was talking about IValidatableObject in DataAnnotations and how ASP.NET MVC 3 has direct support for it: ASP.NET MVC 3 Validation and IValidatableObject. I mentioned that you can use IValidatableObject for those validation rules that are based on the entire model. The example I showed ( with caveats ) was for validating that ConfirmPassword and Password were the same during user registration. I also mentioned that you can build your own custom ValidationAttribute in .NET Framework 4 to do this since there is an overload of IsValid that passes in the whole object being validated as a part of ValidationContext.
If you thought I was setting you up for something, I was :) In ASP.NET MVC 3 there is a new CompareAttribute that helps us in such situations and supports client-side validation to boot!
CompareAttribute in ASP.NET MVC 3
I think a picture is worth a thousand words here, so let's take that same RegisterRequest Class from yesterday and add the CompareAttribute in ASP.NET MVC 3 to the ConfirmPassword Property as such:

The CompareAttribute is perfect for these situations in which we want to compare that one property equals another. In this case, we have a validation rule that says the ConfirmPassword must equal the Password Property. If they don't match, we can specify an error message, in this case “Passwords Must Match.”
The beauty of this custom ValidationAttribute is that it already has support for client-side validation! Therefore, the user gets immediate feedback that what he or she typed in for ConfirmPassword does not equal what was typed in for Password.

Conclusion
If you need to compare that one property equals another in ASP.NET MVC 3 during validation, leverage the new CompareAttribute!
Happy New Year!
David Hayden
ASP.NET MVC 3 Tutorials