5

我正在尝试将管理员添加到我的网站。在我的 AccountsModels.cs 上发现了问题

它只需要比较实现的数据,但我似乎得到了这个错误。

我也有一个观点:
-Register.cshtml
-LogOn.cshtml
-ChangePasswordSuccess.cshtml
-ChangePassword.cshtml

当然还有一个 AccountController.cs..

有人知道解决方案吗?

这是代码:

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; }

    }
}
4

8 回答 8

5

从您的代码看来,您希望将密码与确认密码进行比较。如果是这样,那么你的属性

Compare

是不正确的。它应该是

[CompareAttribute("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]

您已经在代码中添加了所需的命名空间。你可以在这里阅读更多关于它的信息。

于 2013-08-17T12:25:50.783 回答
2

我遇到此错误的情况是我尝试从 4.0 升级到 .Net 框架 4.5。这不起作用,我恢复到 4.0 并收到此错误。

我通过输入修复了这个构建错误

[System.Web.Mvc.Compare]

于 2014-06-24T08:05:15.707 回答
2

我也有同样的错误。我意识到,当我使用 .net 4.0System.Web.Mvc;并使用它时,System.ComponentModel.DataAnnotations;它已解决。但是当我更改框架 o 4.5 时,错误是

'CompareAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

The type or namespace name 'Compare' could not be found (are you missing a using directive or an assembly reference?)

结论如果框架是 4.0

System.Web.Mvc;

System.ComponentModel.DataAnnotations;

如果框架是 4.0,则重新构建项目

System.ComponentModel.DataAnnotations;

并重建了项目

不要忘记始终以管理员身份(模式)运行 Visual Studio

于 2013-12-20T22:16:56.070 回答
1

如果你使用的是Web API类,去掉“using System.Web.Mvc;” 并更改为“使用 System.Web.Http;” 并保持“比较”关键字不变。但如果您使用纯 MVC,请将“Compare”关键字更改为“CompareAttribute”。

System.ComponentModel.DataAnnotations; 这两种情况都需要进行验证。但是“比较”或“比较属性”并不是纯粹用于验证。它是系统基于其架构模式(MVC 或 RestFul Web API)提供的一种工具。它使用属性类来简化引用和使用“比较工具”

于 2014-09-11T00:15:30.790 回答
1

.NET 中有 2 个属性 可能System.Web.Mvc.CompareAttribute位于程序集中的命名空间中的一个System.Web.Mvc.dll可以完成这项工作。

还有另一个(重复的)做同样的事情。如我所见,完整路径System.ComponentModel.DataAnnotations.CompareAttribute在程序集中,这是您引用的路径。System.ComponentModel.DataAnnotations.dll那么您可能需要对 DLL 的引用System.ComponentModel.DataAnnotations.dll

您可以检查您是否没有同时引用两者,因为这也会导致问题。不过,这似乎不是问题所在,我只是提一下。

于 2013-08-17T12:48:57.883 回答
1

要在 .NET4 和 .NET45 之间进行这项工作,您需要将这些文件中的两个 using 语句更改为:

using System.ComponentModel.DataAnnotations;
using CompareAttribute = System.Web.Mvc.CompareAttribute;
于 2015-09-01T10:14:25.043 回答
0

很简单,以命名空间为前缀:

System.ComponentModel.DataAnnotations.Compare

由于所有其他属性(如 [Required]、[Display] 等)都使用此命名空间,因此这是有道理的。

编译器会感到困惑,因为 System.Web.Mvc 命名空间也有一个名为“Compare”的方法,因此 Framework 4.5 希望您明确并消除歧义。如果将光标悬停在其他属性上,您将看到它们使用 System.ComponentModel.DataAnnotations 命名空间,但不需要限定,因为它们与其他命名空间不冲突,而 [Compare] 存在于两个命名空间中。有趣的是框架 4 是如何工作的,但框架 4.5 需要被告知......

于 2014-10-22T02:59:47.803 回答
0

添加对 System.Web.Mvc.dll 程序集的引用!

于 2013-08-17T12:24:02.337 回答