Ejemplo n.º 1
0
        public async Task<ActionResult> Edit(UsersAdminEditViewModel model)
        {
            if (model.UserId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);              
            }

            var user = await UserManager.FindByIdAsync(model.UserId);
            user.UserName = model.UserName;
            user.PhoneNumber = model.PhoneNumber;

            if (ModelState.IsValid)
            {
                //update the user details
                await UserManager.UpdateAsync(user);

                //if user has existing Role then remove the user from the role
                //this also accounts for the case when the Admin selected Empty from the drop-down and
                //this means that all roles for the user must be removed
                var currentRolesForUser = await UserManager.GetRolesAsync(user.Id);
                if (currentRolesForUser.Count() > 0)
                {
                    foreach (var item in currentRolesForUser)
                    {
                        var result = await UserManager.RemoveFromRoleAsync(user.Id, item);
                    }
                }

                if (model.RolesIdForUser!=null)
                {
                    foreach(var item in model.RolesIdForUser)
                    {
                        //find role
                        var role = await RoleManager.FindByIdAsync(item);
                        //add user to new role
                        var result = await UserManager.AddToRoleAsync(user.Id, role.Name);
                        if (!result.Succeeded)
                        {
                            ModelState.AddModelError("", result.Errors.First().ToString());
                            return View(model);
                        }
                    }
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View(model);
            }
            
        }      
Ejemplo n.º 2
0
        //Get: /Users/Edit/1
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        
            var user = await UserManager.FindByIdAsync(id);
            if(user==null)
            {
                return HttpNotFound();
            }

            //for every RoleName find it's RoleId
            var rolesNameList = await UserManager.GetRolesAsync(user.Id);
            var rolesIdList = new List<string>();
            foreach(var item in rolesNameList)
            {
                rolesIdList.Add(RoleManager.FindByName(item).Id);
            }
            
            var editVM = new UsersAdminEditViewModel
            {
                UserId = user.Id,
                UserName = user.UserName,
                Email=user.Email,
                PhoneNumber = user.PhoneNumber,
                RolesIdForUser = rolesIdList.ToArray(),
                Roles = await RoleManager.Roles.ToListAsync()
            };

            return View(editVM);
        }