Asp.net mvc 5 ';确认密码';和';密码';不匹配。MVC5

Asp.net mvc 5 ';确认密码';和';密码';不匹配。MVC5,asp.net-mvc-5,Asp.net Mvc 5,我无法本地化验证:“确认密码”和“密码”不匹配。在MVC5中 [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string

我无法本地化验证:“确认密码”和“密码”不匹配。在MVC5中

[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.")] //Why not display this message???????
        public string ConfirmPassword { get; set; }

请帮助我将其本地化。

这似乎是一个已知问题,目前无法正常工作-

但是,临时解决方法是使用System.Web.Mvc中的Compare属性,该属性被标记为过时。以下是一个例子:

using CompareObsolete = System.Web.Mvc.CompareAttribute;

...

[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")]
[CompareObsolete("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
在正式修复可用之前,我目前正在使用此解决方案。一切都运行得很好-我使用这个属性使用资源来本地化错误消息

只是别忘了在正式补丁发布后更新它


编辑:此问题已在最新版本中修复。

您有两个选项来解决此错误:

--选项1

更改:

[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]

--选项2(我推荐这一个)

我们需要更新我们的ASP.NETMVC5。在Visual Studio中,转到Package Manager控制台并键入:

PM> update-package
您可能会在以下内容中遇到错误:

public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

该错误是由MVC5内部结构中的更新引起的。要解决此错误,请执行以下操作:

似乎有两种类型的CompareAttribute。看看MSDN,名称空间
System.Web.Mvc
的那个似乎已经过时了,他们建议使用名称空间
System.ComponentModel.DataAnnotations
的那个。 链接:

对于Visual Studio,您必须显式地使用注释,并将名称空间添加到注释中的属性,如下所示:

[System.ComponentModel.DataAnnotations.CompareAttribute("Password", ErrorMessage = "The password and confirmation password do not match.")]

有关更多信息,请参见:

本地化意味着根据特定语言定制代码。你是说国际化吗?
[System.ComponentModel.DataAnnotations.CompareAttribute("Password", ErrorMessage = "The password and confirmation password do not match.")]