0

我的问题与此问题类似,但我仍然在服务器端和客户端的验证方面遇到问题。我想对设置在不同模型中的两个属性进行比较。

我的模型如下:

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>
4

3 回答 3

1

这对我有用(适用于:客户端和服务器端)。

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>  
于 2013-11-09T19:52:44.897 回答
-1

在您的注册视图中,您需要引用用户模型的密码,而不是 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; }
}
于 2013-11-09T19:01:52.747 回答
-1

你能检查一下这篇文章吗?我认为它会帮助你。

例子:

public class RegisterModelValidator : AbstractValidator<RegisterModel>
{
    public RegisterModelValidator()
    {
        RuleFor(x => x.UserName)
            .NotNull();
        RuleFor(x => x.ConfirmPassword)
            .Equal(x => x.User.Password);
    }
}

而另一篇文章也可以解决这个问题。

于 2013-11-09T19:18:31.283 回答