コード例 #1
0
        public async Task<IActionResult> Edit(EditUserViewModel editedUser)
        {
            //Sync
            var user = await _userManager.FindByIdAsync(editedUser.User.Id);
            user.UserName = editedUser.User.UserName;
            user.Email = editedUser.User.Email;
            user.PhoneNumber = editedUser.User.PhoneNumber;

            //Update User
            var result = await _userManager.UpdateAsync(user);
            if (result.Succeeded)
            {
                if(await _identityManager.ClearUserRoles(user.Id))
                {
                    if (editedUser.SelectedRoles != null)
                    {
                        foreach (var role in editedUser.SelectedRoles)
                        {
                            await _identityManager.AddUserToRole(user.Id, role);
                        }
                    }
                }
                
                return RedirectToAction(nameof(AccountController.Index), "Account");
            }

            return View(editedUser);
        }
コード例 #2
0
        public async Task<IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            var user = _userManager.Users.Include(u => u.Roles).FirstOrDefault(u => u.Id == id);
            if (user == null)
            {
                return HttpNotFound();
            }

            var userViewModel = new EditUserViewModel()
            {
                User = user,
                SelectedRoles = from role in user.Roles
                                select role.RoleId
            };

            ViewBag.AllRoles = await _identityManager.GetRoles();

            return View(userViewModel);
        }