加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在MVC视图上没有客户端密码匹配

发布时间:2020-12-16 07:35:40 所属栏目:asp.Net 来源:网络整理
导读:我有以下(删节)DTO注册新用户: [PropertiesMustMatch("Password","ConfirmPassword",ErrorMessage = "The password and confirmation password do not match.")]public class RegisterModel{ //..... [DataType(DataType.Password)] public string Password
我有以下(删节)DTO注册新用户:

[PropertiesMustMatch("Password","ConfirmPassword",ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterModel
{
    //.....
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }
}

然后将其包装在View Model中:

public class RegisterModelViewData: BaseViewData
{
    public RegisterModel RegisterModel { get; set; }
    public int PasswordLength { get; set; }
}

最后,在视图中,我有两个字段:

<div class="editor-field">
    <%= Html.PasswordFor(m => m.RegisterModel.Password) %>
    <%= Html.ValidationMessageFor(m => m.RegisterModel.Password) %>
</div>
<div class="editor-field">
    <%= Html.PasswordFor(m => m.RegisterModel.ConfirmPassword) %>
    <%= Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword) %>
</div>

显然,如果密码不匹配,我应该得到客户端验证,没有帖子.我得到一个帖子,然后收到一条消息“帐户创建失败”,但没有关于密码不匹配的信息.为简洁起见,我在此处省略了密码proeprties中的Required和MininumLength属性,但它们似乎按预期运行并在客户端上进行验证.

解决方法

现在可以在ASP.MVC 3中使用,以防任何人仍然想知道

public string Password { get; set; }
[Compare("Password",ErrorMessage = "Passwords must match")]
public string ConfirmPassword { get; set; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读