-
-
Save skynode/a09d814da1f6c639c3e83c18911b4fdb to your computer and use it in GitHub Desktop.
ValidationSummaryMultipleForms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace project | |
{ | |
// ... | |
public class AccountsController : Controller { | |
// ... | |
[HttpGet, Route("accounts/manage/change-passwords", Name="change-password")] | |
[Authorize(Roles="Admin")] | |
public ActionResult ManagePasswords() { | |
var users = this.usersStore.GetAllUsers(); | |
var passwordEditorsList = new List<PasswordEditorViewModel>(); | |
foreach(var user in users) { | |
passwordEditorsList.Add(new PasswordEditorViewModel { Username = user.Username }); | |
} | |
var passwordEditorsListViewModel = new PasswordEditorsListViewModel(passwordEditorsList); | |
return View(viewName: "~/views/accounts/manage-passwords.cshtml", model: passwordEditorsListViewModel); | |
} | |
[HttpPost] | |
[Route("accounts/manage/change-password", Name="change-password-request")] | |
[Authorize(Roles="Admin"), ValidateAntiForgeryToken] | |
public ActionResult ChangePassword(PasswordEditorViewModel model) { | |
if(this.ModelState.IsValid) { | |
var passwordChangeResult = this.passwordManagementService.ChangePassword(model.Username, model.OldPassword, model.NewPassword); | |
if(passwordChangeResult.ResultCode != PasswordChangeResultCode.Success) { | |
var modelPrefix = this.ModelState.Keys.First().Split(new char[]{'.'})[0]; | |
this.ModelState.AddModelError( | |
key: modelPrefix + ".PasswordChangeError." + passwordChangeResult.ResultCode.ToString(), | |
errorMessage: passwordChangeResult.ErrorMessage | |
); | |
} | |
else { | |
this.TempData["PasswordChangeMessage"] = string.Format("Password for user '{0}' was successfully changed", model.Username); | |
if(model.Username == "Admin") { | |
this.OwinAuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); | |
return RedirectToAction("login"); | |
} | |
} | |
} | |
return ManagePasswords(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace project | |
{ | |
using System.Web.Mvc; | |
public class CollectionEditorModelBinder : DefaultModelBinder { | |
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { | |
bindingContext.ModelName = controllerContext.HttpContext.Request.Form["model.prefix"]; | |
return base.BindModel(controllerContext, bindingContext); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace project | |
{ | |
public class PasswordEditorsListViewModel { | |
public PasswordEditorsListViewModel(IEnumerable<PasswordEditorViewModel> passwordEditors) { | |
this.PasswordEditors = passwordEditors; | |
} | |
public IEnumerable<PasswordEditorViewModel> PasswordEditors { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace project | |
{ | |
using System.ComponentModel.DataAnnotations; | |
public class PasswordEditorViewModel { | |
[Required] | |
public string Username { get; set; } | |
[Required, DataType(DataType.Password)] | |
public string OldPassword { get; set; } | |
[Required, DataType(DataType.Password)] | |
public string NewPassword { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model PasswordEditorViewModel | |
@* | |
as it is suggested in the http://stackoverflow.com/questions/18102918/multiple-forms-in-mvc-view-modelstate-applied-to-all-forms | |
post the editor template is located at ~/views/shared/EditorTemplates and has the same name as the corresponding view model | |
(PasswordEditorViewModel) | |
*@ | |
@using(Html.BeginRouteForm(routeName: "change-password-request")) { | |
@Html.AntiForgeryToken() | |
<input type="hidden" name="model.prefix" value="@ViewData.TemplateInfo.HtmlFieldPrefix" /> | |
<h4>@Model.Username</h4> | |
@Html.HiddenFor(i => i.Username) | |
<div class="control-group"> | |
<label>old password</label> | |
@Html.PasswordFor(i => i.OldPassword, new { placeholder = "enter old password" }) | |
</div> | |
<div class="control-group"> | |
<label>new password</label> | |
@Html.PasswordFor(i => i.NewPassword, new { placeholder = "enter new password" }) | |
</div> | |
@if(!ViewData.ModelState.IsValid) { | |
<ul class="validation-errors"> | |
@* the below thing works *@ | |
@* | |
<li class="validation-error">@Html.ValidationMessageFor(i => i.OldPassword)</li> | |
<li class="validation-error">@Html.ValidationMessageFor(i => i.NewPassword)</li> | |
<li class="validation-error">@Html.ValidationMessage("PasswordChangeError.WrongPassword")</li> | |
*@ | |
@* and this one is broken *@ | |
@Html.ValidationSummary() | |
</ul> | |
} | |
<button type="submit">Submit Changes</button> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment