数据库创建“用户表”“角色表”“用户角色关系表”

create table roles
(
RId int identity,
RName varchar(),
Remark varchar()
)
create table UserRole
(
Users_UId int,
roles_Rid int
)
create table Users
(
UId int identity,
UName varchar(),
UPwd varchar()
)

数据库创建一个view视图

create view USER_SHOW
AS
select RName,RId,UName,UId from Users join UserRole on Users.UId=UserRole.Users_UId join roles on UserRole.roles_Rid=roles.RId

然后打开VS创建MVC

添加一个控制器

控制器需要引用

using Dapper;
using System.Data.SqlClient;

控制器代码如下

public ActionResult Index()
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
{
List<UserAndRole> list = conn.Query<UserAndRole>("select UId,UName,stuff((select ','+RName from USER_SHOW where a.UId = UId for xml path('')),1,1,'') as RName from USER_SHOW as a group by UId,UName").ToList();
return View(list);
}
} // GET: User
public ActionResult Shezhi(int Uid)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
{
Session["Uid"] = Uid;
ViewBag.list = GetBind();
List<UserAndRole> list = conn.Query<UserAndRole>($"select RId,RName from Users join UserRole on Users.UId = UserRole.Users_UId join roles on UserRole.roles_Rid = roles.RId where UId = {Uid}").ToList();
return View(list);
}
}
public List<UserAndRole> GetBind()
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
{
return conn.Query<UserAndRole>("select * from roles ").ToList();
}
} public int Delete(int Rid)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
{
return conn.Execute($"delete from UserRole where roles_Rid={Rid}");
}
} public int Add(string UId, string RId)
{
UId = Session["Uid"].ToString();
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
{
object n = conn.ExecuteScalar($"select count(1) from UserRole where Users_UId={UId} and roles_Rid={RId}");
if (Convert.ToInt32(n) == )
{
return conn.Execute($"insert into UserRole values('{UId}','{RId}')");
}
else
{
return ;
} }
} public class UserAndRole
{
public int UId { get; set; }
public string UName { get; set; }
public string RName { get; set; }
public int RId { get; set; } }

然后创建Index视图(

  1. 页面显示雇员信息
  2. 点击“设置角色”跳转Shezi页面为以下部分赋值

(1) 右侧显示的是所有“角色”

(2) 左侧显示的是当前雇员 现有的角色)

@using 配置角色.Controllers
@model List<UserController.UserAndRole>
@{
ViewBag.Title = "Index";
} <table class="table-bordered table">
<tr>
<td>编号</td>
<td>雇员姓名</td>
<td>角色</td>
<td></td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.UId</td>
<td>@item.UName</td>
<td>@item.RName</td>
<td> <a href="/User/Shezhi?Uid=@item.UId">设置角色</a></td>
</tr>
}
</table>

运行效果

再添加一个Shezhi视图

@{
ViewBag.Title = "Shezhi";
}
@using 配置角色.Controllers
@model List<UserController.UserAndRole> <div id="app" style="height:250px;width:100%;border:double">
<div style="height:150px;width:250px;border:double;float:left;margin-top:45px;margin-left:20px">
<span>所有可选角色:</span>
<select id="Select1" multiple="true">
@foreach (var item in ViewBag.list as List<UserController.UserAndRole>)
{
<option value="@item.RId">@item.RName</option>
} </select>
</div>
<div style="height:150px;width:150px;float:left;margin-top:80px;margin-left:25%">
<button onclick="Zuo()">←</button>
<br>
<button onclick="You()">→</button>
</div>
<div style="height:150px;width:250px;border:double;float:right;margin-top:45px;margin-right:20px">
<span>当前雇员所属角色:</span>
<select id="Select2" multiple="true">
@foreach (var item in Model)
{
<option value="@item.RId">@item.RName</option>
} </select> <input id="Hidden1" type="@Session["Uid"]" />
</div>
</div> <script>
function Zuo() {
//alert(1);
var id = $("#Select2").val();
if (id == null) {
alert('请选择')
}
else {
$.ajax({
url: "/User/Delete?rid=" + id,
success: function (d) {
if (d > ) {
alert('成功');
}
} }) } }
function You() {
//alert(1); var UId = $("#Hidden1").val();
var RId = $("#Select1").val(); $.ajax({
url: "/User/Add?Uid=" + UId + "&RId=" + RId,
success: function (d) {
if (d > ) {
alert('成功');
}
else {
alert('用户已存在');
}
} })
} </script>

实现效果

(1) 右侧选择了,再点击中部的一个按钮可以删除

(2) 左侧的选择了,再点击中部的另一个按钮可以添加到左侧

C#MVC实现为雇员配置角色(完整详细+数据库)的更多相关文章

  1. Spring MVC 学习总结(一)——MVC概要与环境配置 转载自【张果】博客

    Spring MVC 学习总结(一)--MVC概要与环境配置   目录 一.MVC概要 二.Spring MVC介绍 三.第一个Spring MVC 项目:Hello World 3.1.通过Mave ...

  2. ASP.NET MVC +EasyUI 权限设计(四)角色动作

    请注明转载地址:http://www.cnblogs.com/arhat 由于最近的事情比较多,一直忙于工作和照顾老婆,所以老魏更新的速度慢了,本来写文章就要占据工作和生活很多的时间,这也就是院子中很 ...

  3. 【转】android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)

    原文网址:http://www.cnblogs.com/zdz8207/archive/2012/11/27/android-ndk-install.html android 最新 NDK r8 在w ...

  4. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  5. android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创)

      android 最新 NDK r8 在window下开发环境搭建 安装配置与使用 详细图文讲解,完整实际配置过程记录(原创) 一直想搞NDK开发却一直给其他事情耽搁了,参考了些网上的资料今天终于把 ...

  6. Spring MVC 的xml一些配置

    1.可以自动加载注解驱动,通过注解找到对应Controller <!-- spring MVC 注解驱动 --> <mvc:annotation-driven></mvc ...

  7. Spring MVC 使用tomcat中配置的数据源

    Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...

  8. Nginx配置Https(详细、完整)

    Nginx配置Https(详细.完整) 原文链接:请支持原创 前置条件: 在配置https之前请确保下面的步骤已经完成 服务器已经安装nginx并且通过http可以正常访问 不会安装nginx的可以参 ...

  9. Redis安装配置与Jedis访问数据库

    一.NOSQL概要 NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库.NoSQL数据库的四大分类 键值(Key-Value)存储数据库 这一类数据 ...

随机推荐

  1. java容器(一) Collection类框架图解

  2. iNeuOS工业互联平台,iNeuKernel(物联网核心组件)远程控制标准化设计与实现。发布v2.3版本。

    目       录 1.      概述... 2 2.      平台演示... 2 3.      控制端与iNeuKernel的交互协议... 3 4.      设备驱动实现控制业务... 4 ...

  3. SpringBoot学习遇到的问题(1) - 配置文件有日志的debug模式等配置项,为什么不起作用

    这个问题困扰我近乎两天,通过查找N多资料后终于解决,写下来共享给大家. logging.level.root=DEBUG ... 一系列的日志配置项,都不起作用的原因是springboot启动加载不到 ...

  4. SpringBoot图文教程「概念+案例 思维导图」「基础篇上」

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 每个知识点配套自测面试题,学完技术自我测试 本文初学向,所以希望文中所有的代码案例都能敲一遍 大哥大姐 ...

  5. POJ_3627_贪心

    题目描述: 给你N个数和一个总和,要求求出最少个数的数相加大于等于这个总和. 思路: 很简单的贪心,先排序,从大到小加一次,比较一次,直到符合条件. 我用了优先队列,运行时间好像多了一倍= = #in ...

  6. git push 的解决方案

    如果输入$ git push origin master 提示出错信息: 或者 失败的原因:不能 push 远端仓库 原因分析:由于你当前分支落后与远程端对应分支,所以无法更新: 解决方案:使用 gi ...

  7. node - MongoDB数据库

    mongod 安装配置 在Mongodb官网下载最新版本的Mongodb下载地址 下载msi的window安装包,可以装到C盘或者D盘目录下 配置 由于我是安装在D盘的环境下 D:\Program F ...

  8. 消息队列MQ集合

    消息队列MQ集合 消息队列简介 kafka简介 Centos7部署zookeeper和Kafka集群 .

  9. postman简单接口测试

    Postman简单接口测试 1. get请求: a. 选择get请求时,地址栏输入地址,如果需要添加参数,可以直接在地址栏加?后面写参数,也可以在点击params添加参数 b. 在headers中添加 ...

  10. 使用docker搭建FastDFS

    拉取镜像(使用docker-componse可以忽略) [root@localhost ~]# docker pull phinexdaz/fdfs_tracker [root@localhost ~ ...