分配权限MVC DAL+控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Day13ExamMVC.Models;

namespace Day13ExamMVC.DAL
{
public class UserDAL
{
///
/// 列表显示
///
///
public List ShowUserRole()
{
string sql = @“select u.UId,UName,RName from UserInfo u
left join UserRole t on u.UId=t.UId
left join RoleInfo r on r.RId=t.RId”;
return DBHelper.GetList(sql);
}

    /// <summary>
    /// 获取所有角色
    /// </summary>
    /// <returns></returns>
    public List<RoleInfo> GetRoleInfos()
    {
        string sql = "select * from RoleInfo";
        return DBHelper.GetList<RoleInfo>(sql);
    }

    /// <summary>
    /// 获取当前用户的角色
    /// </summary>
    /// <returns></returns>
    public List<UserRole> GetUserRolesById(int uId)
    {
        string sql = "select * from RoleInfo r join UserRole t on r.RId=t.RId where UId=" + uId;
        return DBHelper.GetList<UserRole>(sql);
    }

    /// <summary>
    /// 分配权限
    /// </summary>
    /// <param name="mod"></param>
    /// <returns></returns>
    public int AddUserRole(UserRole mod)
    {
        string sql = $"insert into UserRole values ({mod.UId},{mod.RId})";
        return DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    /// 取消该角色权限
    /// </summary>
    /// <param name="uId">用户Id</param>
    /// <param name="rId">角色Id</param>
    /// <returns></returns>
    public int DelUserRole(int uId,int rId)
    {
        string sql = $"delete from UserRole where UId={uId} and RId={rId}";
        return DBHelper.ExecuteNonQuery(sql);
    }
}

}

//控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Day13ExamMVC.DAL;
using Day13ExamMVC.Models;

namespace Day13ExamMVC.Controllers
{
public class UserController : Controller
{
UserDAL dal = new UserDAL();

    // GET: User
    public ActionResult Main()
    {
        return View();
    }

    /// <summary>
    /// 列表显示
    /// </summary>
    /// <returns></returns>
    public ActionResult ShowUserRole()
    {
        var list=dal.ShowUserRole();
        return Json(list, JsonRequestBehavior.AllowGet);
    }

    /// <summary>
    /// 获取所有角色
    /// </summary>
    /// <returns></returns>
    public ActionResult GetRoleInfos()
    {
        var list = dal.GetRoleInfos();
        return Json(list, JsonRequestBehavior.AllowGet);
    }

    /// <summary>
    /// 获取当前用户的角色
    /// </summary>
    /// <returns></returns>
    public ActionResult GetUserRolesById(int uId)
    {
        var list = dal.GetUserRolesById(uId);
        return Json(list, JsonRequestBehavior.AllowGet);
    }

    /// <summary>
    /// 分配权限
    /// </summary>
    /// <param name="mod"></param>
    /// <returns></returns>
    [HttpPost]
    public int AddUserRole(UserRole mod)
    {
        return dal.AddUserRole(mod);
    }

    /// <summary>
    /// 取消该角色权限
    /// </summary>
    /// <param name="uId">用户Id</param>
    /// <param name="rId">角色Id</param>
    /// <returns></returns>
    [HttpPost]
    public int DelUserRole(int uId, int rId)
    {
        return dal.DelUserRole(uId, rId);
    }
}

}

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页