请告诉我如何在 ASP.NET MVC2 中应用密码比较验证器和确认密码。请给我一些好的链接或任何样本。
谢谢
比较验证器将接受ControlToValidate
应设置为您的确认密码控件的ControlToCompare
属性,应设置为您的密码控件的属性。DataType
属性也用于设置比较数据类型,您可以将其设置为 true。
此示例直接取自 mvc2 模板和 MvcMusicStore 示例(在 codeplex 上)。
此示例假定您使用的是强类型视图。
[PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public class ChangePasswordModel
{
[Required]
[DataType(DataType.Password)]
[DisplayName("Current password")]
public string OldPassword { get; set; }
[Required]
[ValidatePasswordLength]
[DataType(DataType.Password)]
[DisplayName("New password")]
public string NewPassword { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm new password")]
public string ConfirmPassword { get; set; }
}