Kiểm Tra Lỗi Modelstate

I"m designing a view that allows user to lớn modify their profile và password in 2 different forms.

Bạn đang xem: Kiểm tra lỗi modelstate

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("ModifyProfile", "Account", FormMethod.Post)) // Change profileAnd this is my Model

public class AccountViewModel public User User get; set; // For change profile // For change password public string OldPassword get; set; public string NewPassword get; set; 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 khung, only 3 properties OldPassword, NewPassword, NewPasswordConfirm will be validated. Please help, thank you!



Create a dedicated view Mã Sản Phẩm for update password:

public class ModifyPasswordViewModel public string OldPassword get; set; public string NewPassword get; set; public string NewPasswordConfirm get; set; Create a partial view _ModifyPasswordViewModel.cshtml. Since you display multiple views on the same page, I would use Ajax.BeginForm instead of Html.BeginForm just for modifying password via ajax & not via full khung post.


using (Ajax.BeginForm("ModifyPassword", "Account", new AjaxOptions OnSuccess = "changePasswordSuccess")){
Controller action:

public ActionResult ModifyPassword(ModifyPasswordViewModel model) if ( this.ModelState.IsValid ) ... Phường.S.

Xem thêm: Bảng Ngọc Bổ Trợ Cho Ad Xạ Thủ Mùa 11 Lmht Mới Chuẩn Nhất, #1 Bảng Ngọc Bổ Trợ Cho Ad Mùa 9 Mới

in order for client side validation to work you need to lớn include jquery.validate.js and jquery.validate.unobtrusive.js in the view (layout page) và to enable it in web config:


Use IValidatableObject for custom multiple value validation

public class AccountViewModel : IValidatableObject public string OldPassword get; set; public string NewPassword get; set; public string NewPasswordConfirm get; set; public IEnumerable Validate(ValidationContext validationContext) if (// Do something here) yield return new ValidationResult("Error Message"); public ActionResult ModifyAccount() if (!ModelState.IsValid)


Create a ViewModel with the 3 properties OldPassword, NewPassword, NewPasswordConfirm & receive that as the parameter in the Post version of ModifyPassword:

Class ModifyPasswordView public string OldPassword get;set; public string NewPassword get;set; public bool NewPasswordConfirm get;set;public ActionResult ModifyPassword( ModifyPasswordView passwordView) if ( ModelState.IsValid ) ... The modelstate kiểm tra above sầu only looks at the 3 fields in the ModifyPasswordView Model, regardless of the fields present in the form


Thanks for contributing an answer lớn Stack Overflow!

Please be sure khổng lồ answer the question. Provide details & mô tả your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; bachồng them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy và cookie policy

Not the answer you're looking for? Browse other questions tagged c# or ask your own question.

site thiết kế / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.8.27.40079

Your privacy

By clicking “Accept all cookies”, you agree Staông xã Exchange can store cookies on your device và discthua thảm information in accordance with our Cookie Policy.