已经有一个
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"); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。