Beispiel #1
0
        public async Task <IActionResult> EditRole(string id)
        {
            var role = await roleManager.FindByIdAsync(id);

            if (role == null)
            {
                ViewBag.ErrorMeassage = $"Role with Id { id } cannot be found";
                return(View("NotFound"));
            }

            var model = new EditRoleviewmodel
            {
                Id       = role.Id,
                RoleName = role.Name
            };

            foreach (var user in userManager.Users)
            {
                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    model.Users.Add(user.UserName);
                }
            }
            return(View(model));
        }
Beispiel #2
0
        public async Task <IActionResult> EditRole(EditRoleviewmodel vm)
        {
            var role = await _roleManager.FindByIdAsync(vm.Id);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role Not Found";
                return(View("NOtFound"));
            }
            else
            {
                role.Name = vm.RoleName;
                IdentityResult result = await _roleManager.UpdateAsync(role);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListRoles", "Admin"));
                }
                //to check if role already exists show error msg already taken
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                return(View(vm));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> EditRole(EditRoleviewmodel model)
        {
            var role = await roleManager.FindByIdAsync(model.Id);

            if (role == null)
            {
                ViewBag.ErrorMeassage = $"Role with Id { model.Id } cannot be found";
                return(View("NotFound"));
            }
            else
            {
                role.Name = model.RoleName;
                var result = await roleManager.UpdateAsync(role);

                if (result.Succeeded)
                {
                    return(RedirectToAction("listrole"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }
Beispiel #4
0
        public async Task <IActionResult> EditRole(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role Not Found";
            }
            var model = new EditRoleviewmodel()
            {
                Id = role.Id, RoleName = role.Name
            };

            foreach (var user in _usermanager.Users)
            {
                if (await _usermanager.IsInRoleAsync(user, role.Name))
                {
                    model.UserHasRole.Add(user.UserName);
                }
            }

            return(View(model));
        }