52

MVC 4 Beta project fails to compile after upgrading to .Net 4.5.

This happens due to conflict between System.ComponentModel.DataAnnotations.CompareAttribute and System.Web.Mvc.CompareAttribute

System.ComponentModel.DataAnnotations.CompareAttribute MSDN documentation says:

Provides an attribute that compares two properties.

While System.Web.Mvc.CompareAttribute MSDN documentation says:

Provides an attribute that compares two properties of a model.

What is the difference between the two and when it would be "smarter" to use each of them?

10x.

3
  • Does my answer help? If so, it would be great if you could mark it as the answer. Thanks :) Oct 25, 2012 at 17:13
  • Good question. I was wondering which was smarter, too.
    – Josh Mouch
    Mar 28, 2013 at 14:06
  • @Josh please see my answer for more detail on the subject.
    – joelmdev
    Aug 28, 2013 at 21:33

6 Answers 6

38

So, looking at the MSDN documentation and doing a literal comparison of the two classes, I noticed both classes are derived from System.ComponentModel.DataAnnotations.ValidationAttribute. In fact, the classes are almost exactly the same. The only notable difference is that the MVC version also implements IClientValidatable which adds the following properties:

  • FormatPropertyForClientValidation - (static member) Formats the property for client validation by prepending an asterisk and a dot.
  • GetClientValidationRules - Gets a list of compare-value client validation rules for the property using the specified model metadata and controller context.

As for which class you should use, if the model will be directly bound to a view, use the MVC version so that you can take advantage of the client-side validation. However, if you're using ViewModels, you can stick with the ComponentModel class and avoid the unnecessary overhead of the additional properties. Your call!

16

Microsoft Connect work-around is:

Posted by GavK on 6/17/2012 at 5:13 AM

I added a full reference to [System.Web.Mvc.Compare(...)] rather than just using [Compare(...)]

Works for me in VS 2012...

4
  • 3
    - still I do not understand what is the main difference between the two. YoYo Jun 27, 2012 at 17:43
  • 1
    As indicated in my answer, the version coming from System.Web.DataAnnotations is extending the ComponentModel version to enable some web client functionality. When properties decorated with the System.Web.Mvc.CompareAttribute are bound to a Razor view, the view will be able to generate some JavaScript validation hooks. However, if you were to use the System.ComponentModel.DataAnnotations.ValidationAttribute to decorate your properties, the view would not be able to perform client-side validation. You would be limited to server-side validation. Jan 11, 2013 at 19:22
  • Didn't answer the question. He's wondering which should be used.
    – Josh Mouch
    Mar 28, 2013 at 14:06
  • It wasn't happy with me removing the using statement so I added the full reference on the member type, e.g. [System.Web.Mvc.Compare()]. When it was happy I used the feature to remove unused using statements, once done I removed the System.Web.Mvc prefix from the member type.
    – Aaron
    Apr 15, 2014 at 10:13
14

Vinney nailed most of it with the exception of which one you should use...

The reason you have a conflict after changing your target framework to 4.5 is because prior to .NET 4.5 there was no CompareAttribute class in the System.ComponentModel.DataAnnotations namespace and the class defined under System.Web.Mvc filled the gap. Therefore, as an example if you were using [Compare] and [Required] attributes in your model class prior to updating your target framework you ended up with a conflict when you upgraded.

Assuming you are not using anything else in the System.Web.Mvc namespace in your model class, you should remove that using statement and let it rely on the System.ComponentModel.DataAnnotations namespace. Unobtrusive client-side validation will continue to work exactly as it did before, just as it does for other attributes that you decorate your model's properties with from the same namespace (eg Required).

1
  • I wasn't aware of this bit, @jm2. Thanks for the correction (and the 'at-a-boy :) Oct 16, 2013 at 19:47
6

If you wish to be explicit about the reference, you can simply add this line:

using CompareAttribute = System.Web.Mvc.CompareAttribute;

1
  • This solution works IF you still need to have the System.Web.Mvc reference as I do in my class. Dec 4, 2013 at 15:51
4

Using Visual Studio 2013 (MVC 5 project, .NET 4.5) the IntelliSense suggests that System.Web.Mvc.CompareAttribute is deprecated.

I used System.ComponentModel.DataAnnotations.CompareAttribute and it works fine. It also does the client-side validation!

2

On this post, they also suggest another solution, which is to move the reference of the preferred namespace for Compare() inside the model's namespace. Eg. if you prefer to use Compare from System.Web.Mvc, use:

using System.ComponentModel.DataAnnotations;

namespace MyProject.MyViewModel
{
    using System.Web.Mvc;

The compiler will search inside the model's namespace first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.