コード例 #1
0
        public async Task <IActionResult> Delete(ApplicationRoleDetailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var roleId = model.Id;

            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                StatusMessage = "Role not found";
                return(View());
            }

            var updateResult = await _roleManager.DeleteAsync(role);

            if (!updateResult.Succeeded)
            {
                throw new ApplicationException($"Unexpected error occurred during deleting the role .");
            }

            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(ApplicationRoleDetailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var roleId = model.Id;

            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                StatusMessage = "Role not found";
                return(View());
            }

            role.Name        = model.RoleName;
            role.Description = model.Description;

            var updateResult = await _roleManager.UpdateAsync(role);

            if (!updateResult.Succeeded)
            {
                throw new ApplicationException($"Unexpected error occurred during updating the role .");
            }

            model.StatusMessage = $"{role.Name} role has been updated";
            return(View(model));
        }
コード例 #3
0
        public async Task <IActionResult> Edit(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                StatusMessage = "Role not found";
                return(View());
            }

            var model = new ApplicationRoleDetailViewModel
            {
                Id          = id,
                RoleName    = role.Name,
                Description = role.Description
            };

            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Create(ApplicationRoleDetailViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var createResult = await _roleManager.CreateAsync(new ApplicationRole
            {
                Name        = model.RoleName,
                Description = model.Description,
                Status      = true
            });

            if (!createResult.Succeeded)
            {
                throw new ApplicationException($"Unexpected error occurred during creating the role .");
            }

            return(RedirectToAction(nameof(Index)));
        }