由於從MVC 3全unobstrusive客戶端驗證依靠 模型,我不喜歡把一個ConfirmPassword財產上我 模型,我該怎麼辦?
一個完全同意你的看法。這就是爲什麼你應該使用視圖模型。然後在您的視圖模型(特別是給定視圖的要求而設計的一類),你可以使用[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");
}
並非所有的視圖模型* Model *中的類型(或類型成員)必須持久化。你的服務器驗證呢? – oleksii
不僅ConfirmPassword,而且密碼不應該被保留。除了有關AutoMapper的說明外,Darin Dimitrov的ViewModel解決方案是正確的。在保存哈希密碼之前,您應該始終確保哈希密碼的安全。 –
達林從來沒有表明他會堅持明文密碼,他只是說他會將視圖模型映射到領域模型,並將其傳遞給存儲庫。我認爲哈希密碼更正確的細節屬於持久性代碼而不是表現代碼(爲什麼我的MVC控制器必須知道安全哈希的細節?)。 –