mvc-百步飞剑-11
说明:为用户分配角色
1.1 Index页面
1.1.1 添加tab
{ id: 'btnSetRoleAction', text: '为角色分配权限', iconCls: 'icon-edit', handler: function () { setUserRole(); } }
1.1.2 添加div并设置为隐藏
<!--为用户分配角色信息-->
<div id="setUserRoleDiv">
<iframe id="setRoleFrame" width="100%" height="100%" frameborder="0"></iframe>
</div>
$("#setUserRoleDiv").css("display", "none");
1.1.3 完善Js方法
//------------04为用户分配权限开始---------------// function setUserRole() { var rows = $('#tt').datagrid('getSelections');//获取所选择的行 if ( rows.length != 1) { //alert("请选择要修改的商品!"); $.messager.alert("提醒", "请选择要一项角色!", "error"); return; } $("#setUserRoleDiv").css("display", "block"); $("#setRoleFrame").attr("src", "/UserInfo/SetUserRoleInfo/?id=" + rows[0].ID); $("#setUserRoleDiv").dialog({ title: '为用户分配角色', width: 300, height: 400, collapsible: true, maximizable: true, resizable: true, modal: true, buttons: [{ text: 'Ok', iconCls: 'icon-ok', handler: function () { //提交表单 //调用子窗口的方法 var childWindow = $("#setRoleFrame")[0].contentWindow;//获取嵌入在iframe中的子窗体的window对象 childWindow.subEditForm();//调用子窗体中的方法,完成表单提交 } }, { text: 'Cancel', handler: function () { $('#setUserRoleDiv').dialog('close'); } }] }); } //------------04为用户分配权限结束---------------//
1.2 控制器中的后台代码
1.2.1 为用户分配角色
#region 06 为用户分配角色 public ActionResult SetUserRoleInfo() { int id = int.Parse(Request["id"]); //先获取要当前用户信息 var userInfo = UserInfoService.LoadEntities(r => r.ID == id).FirstOrDefault(); ViewBag.RoleInfo = userInfo; short DelFlag = (short)DeleteEnumType.Normal; //显示所有的角色名称 ViewBag.AllRoles = RoleInfoService.LoadEntities(r=>r.DelFlag == DelFlag); //////////------查看用户已经具有什么角色开始------////////// ViewBag.ExtAllRoleIds = from r in userInfo.RoleInfo select r.ID; //////////------查看用户具有什么角色结束------////////// return View(); } #endregion
1.2.2 在config中添加控制器代码
```
两部分代码
//controllers.xml代码 <object type="BBFJ.OA.WebApp.Controllers.RoleInfoController, BBFJ.OA.WebApp" singleton="false" > <!--控制器的属性--> <property name="RoleInfoService" ref="roleInfoService" /> </object> //Service.xml代码 <object type="BBFJ.OA.BLL.RoleInfoService, BBFJ.OA.BLL" singleton="false" name="roleInfoService"> </object>
1.3 添加视图-SetUserRoleInfo
首先,我们将checkBox放入到Ajax.BeginForm表单中.
定义两个集合(存放所有的角色;存放当前用户拥有的角色)
遍历所有的角色(如果遍历当前的角色用户已经具有,则选中复选框)
@{ Layout = null; } @using BBFJ.OA.Model <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>为用户分配角色</title> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript"> function subSetRoleForm() { $("#form1").submit(); } function afterSetUserRole(data) { //调用父窗体中的方法 window.parent.afterSetUserRole(data); } </script> </head> <body> <div> 为用户 @{ UserInfo userInfo = (UserInfo)ViewBag.UserInfo; <span style="font-size:18px;color:red">@userInfo.UName</span> } 分配角色 @{ using (Ajax.BeginForm("SetUserRoleInfo", new { }, new AjaxOptions() { OnSuccess = "afterSetUserRole", HttpMethod = "post" }, new { id = "form1" })) { //提交表单的同时将用户编号提交.SetUserRoleInfo方法拿到用户ID后就知道给那个用户分配角色了 <input type="hidden" name="userId" value="@userInfo.ID" /> //隐藏域 List<RoleInfo> roleInfoList = (List<RoleInfo>)ViewBag.AllRoles; //所有角色 List<int> ExtRoleIdList = (List<int>)ViewBag.ExtAllRoleIds; //用户已经具有的角色 foreach (var roleInfo in roleInfoList) { //创建文本框 string roleName = "cba_" + roleInfo.ID; //判断用户是否具有此角色 if (ExtRoleIdList.Contains(roleInfo.ID)) { <input type="checkbox" name="@roleName" value="@roleInfo.ID" checked="checked"/>@roleInfo.RoleName } else { <input type="checkbox" name="@roleName" value="@roleInfo.ID"/>@roleInfo.RoleName } } } } </div> </body> </html>
1.4 控制器中方法
#region 07-为用户分配角色 [HttpPost] public ActionResult SetUserRoleInfo(FormCollection collection) { int userId = int.Parse(Request["userId"]); //获取表单的值 string[] AllKeys = Request.Form.AllKeys; List<int> list = new List<int>(); foreach (string key in AllKeys) { if (key.StartsWith("cba_")) { string roleId = key.Replace("cba_", ""); list.Add(int.Parse(roleId)); } } UserInfoService.SetUserRole(userId, list); return Content("ok"); } #endregion
1.5 UserInfoService中的方法
#region 为用户设置角色 public bool SetUserRole(int userId, List<int> RoleIdList) { var userInfo = this.CurrentDBSession.UserInfoDal.LoadEntities(u=>u.ID == userId).FirstOrDefault(); if (userInfo != null) { //删除当前用户已经具有的角色 userInfo.RoleInfo.Clear(); foreach (int roleId in RoleIdList) { var roleInfo = this.CurrentDBSession.RoleInfoDal.LoadEntities(r=>r.ID == roleId).FirstOrDefault(); //根据RoleIDList集合中的角色编号获取角色信息,然后个当前用户添加 userInfo.RoleInfo.Add(roleInfo); } } return this.CurrentDBSession.SaveChanges(); } #endregion
运行效果:

浙公网安备 33010602011771号