public ActionResult EditUser(string id) { if(id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var user = UserManager.FindById(id); if(user == null) { return HttpNotFound(); } ViewBag.DefaultRole = UserManager.GetRoles(user.Id)[0]; var roles = new SelectList(db.Roles.ToArray(), "Id", "Name"); var editUser = new EditViewModel() { Id = user.Id, UserName = user.UserName, Email = user.Email, PhoneNumber = user.PhoneNumber, Roles = roles, IsActive = user.IsActive }; return View(editUser); }
public async Task<ActionResult> EditUserConfirmed(EditViewModel model, string Role) { if(!ModelState.IsValid) { return RedirectToAction("ListUsers", "AdminPanel"); } var user = UserManager.FindById(model.Id); if(user == null) { return HttpNotFound(); } user.UserName = model.UserName; user.Email = model.Email; user.PhoneNumber = model.PhoneNumber; user.IsActive = model.IsActive; UserManager.RemoveFromRoles(user.Id, UserManager.GetRoles(model.Id).ToArray()); UserManager.AddToRole(user.Id, RoleManager.FindById(Role).Name); await UserManager.UpdateAsync(user); await db.SaveChangesAsync(); return RedirectToAction("ListUsers", "AdminPanel"); }