asp.net-mvc 用于将一个属性与另一个属性进行比较的 MVC 数据注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2450922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC data annotation to compare one property to another?
提问by devlife
I've been playing around data annotations in MVC2 and am curious if there is an annotation to compare 2 properties (ie. password, confirm password)?
我一直在玩 MVC2 中的数据注释,并且很好奇是否有注释来比较 2 个属性(即密码、确认密码)?
采纳答案by Dustin Laine
Here you go: http://www.dotnetguy.co.uk/post/2010/01/09/Property-Matching-With-Data-Annotations.aspx
Edit:New link: http://www.dotnetguy.co.uk/post/2010/01/09/property-matching-with-data-annotations/
给你:http: //www.dotnetguy.co.uk/post/2010/01/09/Property-Matching-With-Data-Annotations.aspx
编辑:新链接:http: //www.dotnetguy.co。英国/post/2010/01/09/property-matching-with-data-annotations/
回答by Cillié Malan
If you are using ASP.Net MVC 3, you can use System.Web.Mvc.CompareAttribute
如果您使用的是 ASP.Net MVC 3,则可以使用 System.Web.Mvc.CompareAttribute
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirm { get; set; }
回答by Mitch
System.Web.Mvc.CompareAttribute has been deprecated.
System.Web.Mvc.CompareAttribute 已被弃用。
I was able to modify to work like this:
我能够修改为这样的工作:
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

