C#MVC实现为雇员配置角色(完整详细+数据库)

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

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

数据库创建一个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 list = conn.Query("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 list = conn.Query($"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 GetBind()
        {
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Unit13;Integrated Security=True"))
            {
                return conn.Query("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) == 0)
                {
                    return conn.Execute($"insert into UserRole values('{UId}','{RId}')");
                }
                else
                {
                    return 0;
                }

            }
        }

        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
@{
    ViewBag.Title = "Index";
}

class="table-bordered table">
    
    @foreach (var item in Model)
    {
        
    }
编号 雇员姓名 角色
@item.UId @item.UName @item.RName "/User/[email protected]">设置角色

运行效果

 

 

再添加一个Shezhi视图

@{
    ViewBag.Title = "Shezhi";
}
@using 配置角色.Controllers
@model List

"app" style="height:250px;width:100%;border:double">
"height:150px;width:250px;border:double;float:left;margin-top:45px;margin-left:20px"> 所有可选角色: <select id="Select1" multiple="true"> @foreach (var item in ViewBag.list as List) { } select>
"height:150px;width:150px;float:left;margin-top:80px;margin-left:25%">
"height:150px;width:250px;border:double;float:right;margin-top:45px;margin-right:20px"> 当前雇员所属角色: <select id="Select2" multiple="true"> @foreach (var item in Model) { } select> "Hidden1" type="@Session["Uid"]" />

实现效果

C#MVC实现为雇员配置角色(完整详细+数据库)_第1张图片

 

 

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

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

C#MVC实现为雇员配置角色(完整详细+数据库)_第2张图片

 

你可能感兴趣的:(C#MVC实现为雇员配置角色(完整详细+数据库))