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

如何在ASP.NET MVC 3中正确实施“确认密码”?

发布时间:2020-12-15 18:49:09 所属栏目:asp.Net 来源:网络整理
导读:已经有一个 answered question关于相同的主题,但从’09我认为它已经过时了。 如何在ASP.NET MVC 3中正确实现“确认密码”? 我在网上看到很多选项,其中大多数使用了like this one型号的CompareAttribute 问题是肯定ConfirmPassword不会在模型中,因为它不
已经有一个 answered question关于相同的主题,但从’09我认为它已经过时了。

如何在ASP.NET MVC 3中正确实现“确认密码”?

我在网上看到很多选项,其中大多数使用了like this one型号的CompareAttribute

问题是肯定ConfirmPassword不会在模型中,因为它不应该被保留。

由于来自MVC 3的整个客户端验证依赖于模型,我不想将ConfirmPassword属性放在我的模型上,该怎么办?

我应该注入一个自定义的客户端验证函数?如果是这样..怎么样?

解决方法

As the whole unobstrusive client validation from MVC 3 rely on the
model and I don’t feel like putting a ConfirmPassword property on my
model,what should I do?

完全同意你的意见。这就是为什么你应该使用视图模型。然后在您的视图模型(专门针对给定视图的要求设计的类)中,您可以使用[Compare]属性:

public class RegisterViewModel
{
    [Required]
    public string Username { get; set; }

    [Required]
    public string Password { get; set; }

    [Compare("Password",ErrorMessage = "Confirm password doesn't match,Type again !")]
    public string ConfirmPassword { get; set; }
}

然后让您的控制器操作采用此视图模型

[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // TODO: Map the view model to a domain model and pass to a repository
    // Personally I use and like AutoMapper very much (http://automapper.codeplex.com)

    return RedirectToAction("Success");
}

(编辑:李大同)

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

    推荐文章
      热点阅读