zoukankan      html  css  js  c++  java
  • mvc给用户设置角色主要步骤

    1.IBLL层

    添加方法声明

    public partial interface IUserInfoService:IBaseService<UserInfo>
    {
    //oh ,my baby,oh ,my baby
    
    IQueryable<UserInfo> LoagPageData(Model.Param.UserQueryParam userQueryParam);
    
    bool SetRole(int userId, List<int> roleIds);
    }
    

      BLL层  方法实现 :现将原来的角色全部删除,在重新添加角色(存放在集合中)

      public bool SetRole(int userId, List<int> roleIds)
            {
                var user = DbSession.UserInfoDal.GetEntities(u => u.ID == userId).FirstOrDefault();
                user.RoleInfo.Clear();//全剁掉。
    
                var allRoles = DbSession.RoleInfoDal.GetEntities(r => roleIds.Contains(r.ID));
                foreach (var roleInfo in allRoles)
                {
                    user.RoleInfo.Add(roleInfo);//加最新的角色。
                }
                DbSession.SaveChanges();
                return true;
            }
    

      2.控制器 UserInfoController

            public ActionResult SetRole(int id)
            {
                //当前要设置角色的用户
                int userId = id;
                
                UserInfo  user= UserInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
    
                //把所有的角色发送 到前台
                ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).ToList();
    
                //用户已经关联的角色发送到前台。
                ViewBag.ExitsRoles = (from r in user.RoleInfo
                                      select r.ID).ToList();
    
                return View(user);
    
            }
    

      3.view层中 设置选择给用户选择角色的界面

    <div>
             <h2>当前给用户:  @Model.UName 设置角色</h2>
            <hr />
            @{
                //所有的角色
                List<LTeasyOA.Model.RoleInfo> allRoles = ViewBag.AllRoles;
                //所有当前用户已经存在的角色
                List<int> existRoleIdList = ViewBag.ExitsRoles;
    
                using (Ajax.BeginForm("ProcessSetRole", "UserInfo", new AjaxOptions() { OnSuccess = "afterSetRole" }))
                {
    
                <input type="hidden" name="UId" value="@Model.ID"/>
                    
                
                    foreach (var roleInfo in allRoles)
                    {
                        string str = "ckb_" +roleInfo.ID;
                        if (existRoleIdList.Contains(roleInfo.ID))
                        {
                            <input type="checkbox" checked="checked" value="@str" name="@str" id="@str"/>
                        }
                        else
                        {
                            <input type="checkbox"  value="@str" name="@str" id="@str"/>
                        }
                        
                        <label for="@str">@roleInfo.Name</label>
                
                        <br />
                     
    
                    }
                }
            }
        </div>
    

      4.提交选择的角色到数据库中

       public ActionResult ProcessSetRole(int UId)
            {
                //第一:当前用户id  --uid
                //第二:所有打上对勾的 角色。 ---> list
                List<int> setRoleIdList =new List<int>();
                foreach (var key in Request.Form.AllKeys)
                {
                    if (key.StartsWith("ckb_"))
                    {
                        int roleId = int.Parse(key.Replace("ckb_", ""));
                        setRoleIdList.Add(roleId);
                    }
                }
    
                UserInfoService.SetRole(UId, setRoleIdList);
                return Content("ok");
    
            }
    

      权限设计数据库设计 关联表会在数据库中自动生成(RoleInfo,R_UserInfo_ActionInfo)

    DbSession是数据库上下文,即联系数据库datebase和.net的中间变量,用已操作数据库的最底层

    类似 Dbcontext 直接可以可取表变量

    在mvc中只是简单的处理一下

     BaseService中CurrentDal只能获取当前Services的类型的Dal

    dbSession在这里理论是取所有的_Dal层表 ,但是在这里 只声明,未赋值,要想取到其他_Dal在这里只能在Spring.net中注册,给DbSession注册所有_DAl或者在_Service中注册只用得到的_Dal,,,,,或者直接在代码中赋值(比较好)

  • 相关阅读:
    hdu 5007 水题 (2014西安网赛A题)
    hdu 1698 线段树(成段替换 区间求和)
    poj 3468 线段树 成段增减 区间求和
    hdu 2795 公告板 (单点最值)
    UVaLive 6833 Miscalculation (表达式计算)
    UVaLive 6832 Bit String Reordering (模拟)
    CodeForces 124C Prime Permutation (数论+贪心)
    SPOJ BALNUM (数位DP)
    CodeForces 628D Magic Numbers (数位DP)
    POJ 3252 Round Numbers (数位DP)
  • 原文地址:https://www.cnblogs.com/lt123/p/7058042.html
Copyright © 2011-2022 走看看