首页 技术 正文
技术 2022年11月19日
0 收藏 786 点赞 2,167 浏览 3732 个字

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

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" rel="external nofollow" >设置角色</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) 左侧的选择了,再点击中部的另一个按钮可以添加到左侧

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918