[英]ASP.NET MVC 4 validation on complex models - model does not validated on server side
我的问题类似于此问题,但是服务器和客户端验证仍然存在问题。 我想对在不同模型中设置的两个属性进行比较。
我的模型如下:
public class User{
public string Password { get; set; }
}
public class UserRegisterViewModel {
public User User{ get; set; }
//This is suggested in linked question - as Compare can only work with local property
public string Password
{
get{return this.User.Password;}
}
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Passwords must match")]
[Required(ErrorMessage = "Confirm password is required")]
[DisplayName("Confirm Password")]
public string CPassword { get; set; }
}
我的控制器动作是:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(UserRegisterViewModel model)
{
if (ModelState.IsValid) //This conditions is false
{
}
return View();
}
它重定向到再次注册页面,并显示验证错误,提示Password must match
。 有人可以帮忙吗? 我检查了这个问题,它有所帮助,但并非完全如此。
如果我像下面那样更改模型结构,则会收到一条错误消息: Could not find a property named User.Password
:
public class UserRegisterViewModel {
public User User{ get; set; }
[DataType(DataType.Password)]
[Compare("User.Password", ErrorMessage = "Passwords must match")]
[Required(ErrorMessage = "Confirm password is required")]
[DisplayName("Confirm Password")]
public string CPassword { get; set; }
}
编辑
我的cshtml页面代码如下。
<p>
@Html.LabelFor(model => model.User.Password)
@Html.PasswordFor(model => model.User.Password, new { @class = "wd189 inputtext" })
@Html.ValidationMessageFor(model => model.User.Password)
</p>
<p>
@Html.LabelFor(model => model.CPassword)
@Html.PasswordFor(model => model.CPassword, new { @class = "wd189 inputtext" })
@Html.ValidationMessageFor(model => model.CPassword)
</p>
这对我有用(对于客户端和服务器端)。
public class UserRegisterViewModel
{
private User _user;
public User User
{
get { return _user = (_user ?? new User()); }
}
public string Password
{
get { return User.Password; }
set { User.Password = value; }
}
[DataType(DataType.Password)]
[System.Web.Mvc.Compare("Password", ErrorMessage = "Passwords must match")]
[Required(ErrorMessage = "Confirm password is required")]
[DisplayName("Confirm Password")]
public string CPassword { get; set; }
}
显然,该视图必须如下所示:
<p>
@Html.LabelFor(model => model.Password)
@Html.PasswordFor(model => model.Password, new { @class = "wd189 inputtext" })
@Html.ValidationMessageFor(model => model.Password)
</p>
<p>
@Html.LabelFor(model => model.CPassword)
@Html.PasswordFor(model => model.CPassword, new { @class = "wd189 inputtext" })
@Html.ValidationMessageFor(model => model.CPassword)
</p>
在您的Register View中,您需要引用用户模型的密码,而不是UserRegisterViewModel模型的密码。 如下所示:
@model UserRegisterViewModel
@Html.LabelFor(model => model.User.Password)
@Html.EditorFor(model => model.User.Password)
@Html.ValidationMessageFor(model => model.User.Password)
@Html.LabelFor(model => model.CPassword)
@Html.EditorFor(model => model.CPassword)
@Html.ValidationMessageFor(model => model.CPassword)
public class User
{
[DataType(DataType.Password)]
public string Password { get; set; }
}
public class UserRegisterViewModel
{
public User User { get; set; }
public string Password{get { return this.User.Password; }}
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Passwords must match")]
[Required(ErrorMessage = "Confirm password is required")]
[DisplayName("Confirm Password")]
public string CPassword { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.