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

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. JQuery--JQuery面向对象编程快速入门-插件开发

    JQuery源码片段分析 (function(window){ /** * jQuery是一个函数 是一个对象 是一个构造器函数 */ var jQuery = function( selector, ...

  2. HTML:一张思维导图搞懂HTML

    HTML常用标签及其用法

  3. python笔记04

    数据类型(二) 今日内容 1.列表 2.元组 内容回顾和补充 1.计算机基础 ①硬件:cpu,内存,硬盘,主板,网卡 ②操作系统:linux,centos, Ubuntu,redhat windows ...

  4. Linux密码策略--设置随机密码

    #!/bin/bash # @Author: HanWei # @Date: -- :: # @Last Modified by: HanWei # @Last Modified -- :: # @E ...

  5. 【WPF学习】第四十三章 路径和几何图形

    前面四章介绍了继承自Shape的类,包括Rectangle.Ellipse.Polygon以及Polyline.但还有一个继承自Shape的类尚未介绍,而且该类是到现在为止功能最强大的形状类,即Pat ...

  6. Linux系统之网络文件共享与数据实时同步实践

    1.实现基于MYSQL验证的vsftpd虚拟用户访问 首先环境说明,数据库服务器是192.168.0.10,vsftpd服务器是192.168.0.30 1)安装vsftpd [root@test-c ...

  7. 安装MinGW出现 mingw-get: *** ERROR *** Get package:

    个人的解决方法: 1.手机开个热点让电脑连上. 2.在Setting里面讲proxy关闭.

  8. VFP9.0的GDI+类的使用

    GDI+你应该不会陌生吧,然而,在VFP里要使用这一技术,可不是一件容易的事,你得学习一大堆API函数.或许,一想到这,你已经望而却步了.不过,从现在起,这一技术不再是豪门旺族的专宠了,我们每一位Fo ...

  9. 如何清理ibdata1

    1, 加锁,然后全备份数据,可以用mysqldump,也可以使用其他的工具: [root@localhost data]# mysqldump --all-databases > /root/a ...

  10. 让div充满整个body

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...