Reputation: 519
I'm trying to add an Admin to my site. There issue is found on my AccountsModels.cs
It simply has to compare the data implemented but I seem to get this error.
I also have a view with:
-Register.cshtml
-LogOn.cshtml
-ChangePasswordSuccess.cshtml
-ChangePassword.cshtml& an AccountController.cs of course..
Someone know a solution?
Here is the code:
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Security;
namespace Videoteek.Domain.Models
{
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
//****
[Required]
[Display(Name = "Security Question")]
public string PwdQuestion { get; set; }
[Required]
[Display(Name = "Security Answer")]
public string PwdAnswer { get; set; }
}
}
Upvotes: 5
Views: 19120
Reputation: 1324
To make this work between .NET4 and .NET45 you need to change your two using statements in those files to:
using System.ComponentModel.DataAnnotations;
using CompareAttribute = System.Web.Mvc.CompareAttribute;
Upvotes: 1
Reputation: 129
Simple, prefix with the namespace:
System.ComponentModel.DataAnnotations.Compare
As all the other attributes like [Required], [Display] etc. all use this namespace then it stands to reason.
The complier gets confused because the System.Web.Mvc namespace also has a method called "Compare", so Framework 4.5 wants you to be explicit and remove the ambiguity. If you hover your cursor over the other attributes you will see they use the System.ComponentModel.DataAnnotations namespace but don't need to be qualified as they do not conflict with other namespaces, whereas [Compare] exists in both namespaces. Funny how Framework 4 works it out but Framework 4.5 needs to be told...
Upvotes: 0
Reputation: 46
If you are using the class for Web API, take out the "using System.Web.Mvc;" and change to "using System.Web.Http;" and leave the "Compare" keyword as it is. But if you are using for pure MVC, change the "Compare" keyword to "CompareAttribute".
System.ComponentModel.DataAnnotations; is needed for both scenario for validations. But "Compare" or "CompareAttribute" is not for validation purely. It is a facility provided by the system based on its architectural pattern (MVC or RestFul Web API). It use attribute class as to simplify of referring and using the "Compare facility"
Upvotes: 1
Reputation: 10193
My circumstances in getting this error was that I tried to upgrade to the .Net framework 4.5 from 4.0. This did not work and I reverted back to 4.0 and got this error.
I fixed this build error by putting in
[System.Web.Mvc.Compare]
Upvotes: 2
Reputation: 21
I had the same error too. And I realized that when I was using .net 4.0 the System.Web.Mvc;
and System.ComponentModel.DataAnnotations;
usings it Resolved. But when I changed the framework o 4.5 the error was
'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'
and
The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?)
Conclusion if the framework is 4.0
System.Web.Mvc;
System.ComponentModel.DataAnnotations;
and rebuilt the project else if the framework is 4.0
System.ComponentModel.DataAnnotations;
and rebuilt the project
Don't forget to always Run Visual Studio as(in) Administrator(mode)
Upvotes: 2
Reputation: 4255
There are 2 Attributes in .NET
Probably the one in the namespace System.Web.Mvc.CompareAttribute
located in the assembly System.Web.Mvc.dll
will do the job.
There is another one (duplicate) which does the same thing. The full path is System.ComponentModel.DataAnnotations.CompareAttribute
in assembly System.ComponentModel.DataAnnotations.dll
which is the one you are referencing as i can see. Then you would probably need a reference to the DLL System.ComponentModel.DataAnnotations.dll
you can check if you haven't referenced both, because this can also lead to problems. it does not seem that this is the problem though, i am just mentioning.
Upvotes: 1
Reputation: 32681
From your code it looks like that you want to compare your password with confirm password. If it is so then your attribute
Compare
is not correct. it should be
[CompareAttribute("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
You already have the desired namespace added in the code. You can read more about it here.
Upvotes: 5