简体   繁体   中英

ASP.NET MVC 4 validation on complex models - model does not validated on server side

My problem is similar to this question but I'm still having problem with validation both server- and client-side. I want to perform a compare on two properties, set in different models.

My models are as follows:

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; }
}

My Controller action is:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(UserRegisterViewModel model)
{
    if (ModelState.IsValid) //This conditions is false
    {
    }
    return View(); 
}

It redirects to again register page with the validation error saying Password must match . Can someone please help ? I have checked this question it helped a bit, but not completely.

If I change the model structure like below, than I get an error saying: 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; }
}

EDIT

My cshtml page code is like below.

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

This worked for me (for both: client and server sides).

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; }
}

EDIT:

and obviously, the view must be as follows:

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

In your Register View, You need to reference the password of the user model, not the password of UserRegisterViewModel model. Like below:

@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; }
}

could you please check this article ? I think that it will help you.

Example:

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

And another one arcticle also can solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM