MVC data annotation to compare one property to another?
I've been playing around data annotations in MVC2 and am curious if there is an annotation to co开发者_JAVA百科mpare 2 properties (ie. password, confirm password)?
If you are using ASP.Net MVC 3, you can use System.Web.Mvc.CompareAttribute
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirm { get; set; }
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/
System.Web.Mvc.CompareAttribute has been deprecated.
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; }
There's not one built in, however, you can make your own. See this link, which shows the "PropertiesMustMatchAttribute" that does just what you're looking for.
精彩评论