【发布时间】:2015-10-08 17:15:22
【问题描述】:
我正在使用 Asp.net MVC 创建密码并确认密码字段。我目前正在使用远程属性来检查密码和确认密码是否相同,但是远程只会在应用它的框被更改时调用一个函数。
我查看了可追溯到去年的以前的帖子,发现最常见的建议是 compare 属性,但现在已弃用。
我假设这个问题有一个预先构建的解决方案。
这是来自模型
[Remote(UserController.ActionNameConstants.PasswordMatch,
UserController.NameConst, AdditionalFields = "ConfirmPassword",
ErrorMessage = "The passwords do not match")]
[MinLength(8, ErrorMessage="Minimum password length is 8")]
[DisplayName("Password"), RequiredIf("Id == 0",
ErrorMessage="Password is required")]
public string Password { get; set; }
[DisplayName("Confirm Password"), RequiredIf("Id == 0",
ErrorMessage = "Confirm password is required")]
public string ConfirmPassword { get; set; }
这是在控制器中
[HttpGet]
public virtual JsonResult PasswordMatch(string password,string confirmPassword)
{
return this.Json(password ==
confirmPassword,JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
所以经过进一步研究,似乎仍有一个未弃用的比较版本,因此任何使用比较的解决方案在使用 System.ComponentModel.DataAnnotation.Compare() 时仍然有效。
标签: c# asp.net-mvc validation password-confirmation