Note that there are some explanatory texts on larger screens.

plurals
  1. POAddModelError for child objects?
    primarykey
    data
    text
    <p>I have a settings page on my site that uses these model classes:</p> <pre><code>public class NameVm { public string GivenName { get; set; } public string FamilyName { get; set; } } public class PasswordVm { public string OldPassword { get; set; } public string NewPassword { get; set; } public string ConfirmPassword { get; set; } } public class AccountVm { public NameVm Name { get; set; } public PasswordVm Password { get; set; } } </code></pre> <p>This is the controller:</p> <pre><code>public class AccountController : Controller { [HttpGet] public override ActionResult Index() { var accountVm = AccountVmFromActiveUser(); return View(accountVm); } [HttpPost] public ActionResult EditName(NameVm vm) { ... } [HttpPost] public ActionResult EditPassword(PasswordVm vm) { if (ModelState.IsValid) { if (!ValidateUser(this.ActiveUser, vm.OldPassword)) { ModelState.AddModelError("????", "Existing password is incorrect."); } else UpdateUserPassword(vm); } var accountVm = AccountVmFromActiveUser(); accountVm.Password = vm; return View(accountVm); } } </code></pre> <p>In my <code>Account.cshtml</code> file I define two forms - one that submits to <code>EditName</code>, one that submits to <code>EditPassword</code>. It's a big file, so here is a small excerpt:</p> <pre><code>@Html.EditorFor(model =&gt; model.Password.OldPassword) @Html.ValidationFor(model =&gt; model.Password.OldPassword) </code></pre> <p>My question is: How do I get <code>ValidationFor</code> to display the error message I add in the <code>EditPassword</code> method? I tried using a key of <code>"Password.OldPassword"</code>, but this didn't work.</p> <p>Alternatively, am I taking the wrong approach here? How should I handle having two forms on the same page? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload