没有[比较]设置密码/确认密码

时间:2015-10-08 17:15:22

标签: c# asp.net-mvc validation password-confirmation

我正在使用Asp.net MVC创建密码并确认密码字段。我目前正在使用远程属性来检查密码和确认密码是否相同,但是如果更改了应用它的框,则远程仅调用函数。

我查看了去年发布的帖子,发现最常见的建议是比较属性,但现在已弃用。

我认为这个问题有一个预先构建的解决方案。

这是来自模型

    [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);
      }

1 个答案:

答案 0 :(得分:1)

比较不是depricate你仍然可以使用[Compare(“Property name to compare with”)] ...它位于“System.ComponentModel.DataAnnotations”命名空间中。