因需要在用户列表中点详情按钮来到当前页,所以需要展示分组详情,并展示当前所属角色组的用户

 public async Task<ActionResult> Details(string id)
{
//查找是否存在角色组
var role = await _roleManager.FindByIdAsync(id);
//如果角色不存在跳转回角色列表
if (role == null)
{
return RedirectToAction(nameof(Index));
}
//给视图模型赋值
var roleUserViewModel = new RoleUserViewModel()
{
RoleId = role.Id,
RoleName = role.Name
};
//找出所有用户
var users = await _userManager.Users.AsNoTracking().ToListAsync();
//循环查找用户是否存在当前角色组
foreach (var item in users)
{
if (await _userManager.IsInRoleAsync(item, role.Name))
{
roleUserViewModel.Users.Add(item);
}
}
return View(roleUserViewModel);
}

详情展示页视图代码如下

@model Shop.ViewModel.RoleUserViewModel

@{
ViewData["Title"] = "Details";
} <h1>Details</h1> <div>
<h4>CreateRoleViewModel</h4>
<hr />
<dl class="row">
<dt class="col-sm-5">
@Html.DisplayFor(model => model.RoleId)
</dt>
<dd class="col-sm-2">
@Html.DisplayFor(model => model.RoleName)
</dd>
</dl>
<dl class="row">
@foreach (var item in Model.Users)
{
<dt>@item.UserName</dt>
}
</dl>
<a asp-action="AddUserToRole" asp-route-id="@Model.RoleId" class="btn btn-success">添加用户到角色</a> </div>
<div>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
<a asp-action="Index">Back to List</a>
</div>

创建UserRoleViewModel模型类

using System.Collections.Generic;
using Microsoft.AspNetCore.Identity; namespace Shop.ViewModel
{
public class UserRoleViewModel
{
public UserRoleViewModel()
{
Users = new List<IdentityUser>();
}
public string RoleId { get; set; }
public string UserId { get; set; }
public List<IdentityUser> Users { get; set; }
}
}

在role控制器中创建添加用户到角色组的显示方法

public async Task<ActionResult> AddUserToRole(string id)
{
//查找是否存在角色
var role = await _roleManager.FindByIdAsync(id);
//如果角色不存在跳回角色列表
if (role == null)
{
return RedirectToAction(nameof(Index));
}
//将查找的角色ID添加到视图模型
var userRoleViewModel = new UserRoleViewModel()
{
RoleId = role.Id
};
//将所有用户找出来
var users = await _userManager.Users.AsNoTracking().ToListAsync();
//循环遍历是否用户不在当前角色中、
foreach (var item in users)
{
if (!await _userManager.IsInRoleAsync(item, role.Name))
{
userRoleViewModel.Users.Add(item);
}
}
//将视图模型返回
return View(userRoleViewModel);
}

根据选择添加用户到角色组

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddUserToRole(UserRoleViewModel input)
{
//查找当前用户
var user = await _userManager.FindByIdAsync(input.UserId);
//查找当前角色组
var role = await _roleManager.FindByIdAsync(input.RoleId);
//角色跟用户都找到
if (user != null && role != null)
{
//用户管理中添加当前用户到角色组(当前用户,角色组名称)
var result = await _userManager.AddToRoleAsync(user, role.Name);
if (result.Succeeded)
{
return RedirectToAction(nameof(Index));
}
//输出所有Model级错误
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
return View(input);
}

页面显示,选择后按添加执行上边方法写入数据库

添加后返回详情页,并显示当前角色组的用户如图所示

添加用户后,再次添加将不再显示在选择框内

删除角色跟添加角色类似,删除代码为_userManager.RemoveFromRoleAsync(user,role.Name)

最新文章

  1. (DFS、全排列)POJ-3187 Backward Digit Sums
  2. ExtJs6.0.0随笔
  3. 【linux】学习7
  4. Android 环境常见问题
  5. Swift - UITableViewCell倒计时重用解决方案
  6. Build Up Your Own Lightweight Workspace
  7. [SQL]SQL语言入门级教材_SQL数据操作基础(二)
  8. 实现RecycleView动态使列表item可以点击或不可点击切换
  9. iOS 开发的几种手势
  10. QT 4.2.2的安装(安装完还要再编译,注意设置Windows Path)
  11. C语言之分支结构 if(一)
  12. mysql 异常处理
  13. 感知器算法--python实现
  14. linux c语言 select函数用法
  15. Automatically populating $HTTP_RAW_POST_DATA is deprecated......
  16. gcc and g++ 常用参数解释
  17. BZOJ3028: 食物(生成函数)
  18. golang gob 有什么优势? gob/protobuf/json/xml 效率对比,benchmark 压力测试
  19. android sdk content loader 0%不动
  20. thinkphp搭建后台品字形框架页面

热门文章

  1. 记录21.07.26 —— Vue/cil
  2. Java基础——逻辑运算符、位运算符
  3. SpringMVC学习07(Ajax)
  4. SpringMVC学习06(JSON)
  5. SpringBoot包扫描之多模块多包名扫描和同类名扫描冲突解决
  6. Longhorn,企业级云原生容器分布式存储 - K8S 资源配置示例
  7. Linux部署Redis服务器
  8. NOIP 模拟 $32\; \rm Walker$
  9. NOIP 模拟 $31\; \rm Time$
  10. 使用npm安装 Ant Design Vue 时报错—ant-design-vue@latest(sha1-qsf / gCIFcRYxyGmOKgx7TmHf1z4 =)seems to be corrupted.