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

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. 面试总结 | Linux后台开发不得不看的知识点(给进军bat的你!)

    目录 一 自我介绍 二 面试情况 三 相关知识点汇总 1 c/c++相关 2 计算机网络 3 数据结构相关 4 数据库相关 5 操作系统 6 Linux基础知识及应用编程(后台必备!) 7 大数问题 ...

  2. 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式

    在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...

  3. 如何准备Java面试?如何把面试官的提问引导到自己准备好的范围内?

    Java能力和面试能力,这是两个方面的技能,可以这样说,如果不准备,一些大神或许也能通过面试,但能力和工资有可能被低估.再仔细分析下原因,面试中问的问题,虽然在职位介绍里已经给出了范围,但针对每个点, ...

  4. mysql随机查询若干条数据

    条不重复的数据,使用以下: 秒以上 搜索Google,网上基本上都是查询max(id) * rand()来随机获取数据. SELECT *  FROM `table` AS t1 JOIN (SELE ...

  5. 使用Apache服务器实现Nginx反向代理

    实验环境:centos7 注:因为本次实验在同一台服务器上,Apache与Nginx同为80端口,所以改Apache端口为60 1 配置Nginx服务器: 编辑Nginx配置文件,写入以下内容 loc ...

  6. 题解【[AHOI2013]作业】

    \[ \texttt{Preface} \] 数据貌似很水,据说 \(A_i\leq n\) ,连离散化都不需要. 不知道为啥设块大小为 \(\frac{n}{\sqrt m}\) 会一直 Runti ...

  7. 微信小程序如何添加新的icon图标

    第一步 先去阿里云下载图标http://www.iconfont.cn/ 根据需要把图片的代码下载下来,下载完成之后是一个 压缩包,解压压缩包里面有一个css的文件复制到项目中,更改后缀为wxss 第 ...

  8. Go语言实现:【剑指offer】题目汇总

    所列题目与牛客网<剑指offer>专题相对应. 数组: 和为S的两个数字 和为S的连续正数序列 连续子数组的最大和 数字在排序数组中出现的次数 数组中只出现一次的数字 旋转数组的最小数字 ...

  9. Go语言实现:【剑指offer】不用加减乘除做加法

    该题目来源于牛客网<剑指offer>专题. 第一步:相加各位的值,不算进位,得到010,二进制每位相加就相当于各位做异或操作,101^111. 第二步:计算进位值,得到1010,相当于各位 ...

  10. TCP加速方式

    使用windows scaling TCP Extensions for High Performance, RFC1323,https://www.ietf.org/rfc/rfc1323.txt ...