User Model being used to edit user
Example #1
0
 /// <summary>
 /// View user details
 /// </summary>
 public ActionResult ViewUserDetail(string id)
 {
     var user = UserManager.FindById(id);
     if (user == null)
         throw new InvalidOperationException("User Not found!");
     var userModel = new UserModelForEditUser
     {
         id = user.Id,
         UserEmail = user.Email,
         PhoneNumber = user.PhoneNumber,
         SelectedRole = user.AspNetRoles.FirstOrDefault().Name
     };
     return View(userModel);
 }
Example #2
0
        public ActionResult EditUser(UserModelForEditUser model)
        {
            var selectedRole = RoleManager.Roles.FirstOrDefault(role => role.Id == model.SelectedRole).Name;
            var user = UserManager.FindById(model.id);
            //user.EmployeeId = model.EmployeeId;
            user.FirstName = model.FirstName;
            user.LastName = model.LastName;

            var userRole = user.AspNetRoles.FirstOrDefault();
            if (userRole != null)
            {
                UserManager.RemoveFromRole(model.id, userRole.Name);
            }
            UserManager.AddToRole(model.id, selectedRole);
            UserManager.Update(user);
            return RedirectToAction("Index");
        }