I'm new to MVC and I'm facing this problem.
I'm designing a view that allows user to modify their profile and password in 2 different forms.
This is my main action:
public ActionResult ModifyAccount()
{
User user = this._userManager.FindById(User.Identity.GetUserId());
ViewBag.User = user;
return View(new AccountViewModel
{
User = user
});
}
In my ModifyAccount view, i have 2 form:
@using (Html.BeginForm("ModifyPassword", "Account", FormMethod.Post))
{
// Change password
}
@using (Html.BeginForm("ModifyProfile", "Account", FormMethod.Post))
{
// Change profile
}
And this is my Model
public class AccountViewModel
{
public User User { get; set; } // For change profile
// For change password
[Display(Name = "Mật khẩu cũ")]
public string OldPassword { get; set; }
[Display(Name = "Mật khẩu mới")]
public string NewPassword { get; set; }
[Display(Name = "Xác nhận mật khẩu mới")]
[Compare("NewPassword", ErrorMessage = "Mật khẩu xác nhận không trùng khớp")]
public string NewPasswordConfirm { get; set; }
}
I know that when we use ModelState.IsValid
, the whole model will be validated. What I want is if the user submits the ModifyPassword form, only 3 properties OldPassword, NewPassword, NewPasswordConfirm will be validated. Please help, thank you!