請注明轉載地址:http://www.cnblogs.com/arhat
由於最近的事情比較多,一直忙於工作和照顧老婆,所以老魏更新的速度慢了,本來寫文章就要占據工作和生活很多的時間,這也就是院子中很多文章都沒寫完就夭折了的原因了,不是因為作者不願意寫,而是身不由己啊。
寫文章不僅鍛煉自身的能力,還能夠把經驗分享給大家,所以貴在堅持啊。如果哪天老魏跟新文章慢了,大家要見諒啊。畢竟寫文章的時候還要做案列,截圖等等,比較慢的,尤其在構思文章內容的時候,可能很多天都想不出來要怎么寫的。
廢話不多說了,接着上一章的內容說。在上一章中我們把基礎模塊寫完了,那么可以再上一章的基礎上呢,可以把動作分為“模塊--動作”,即每個模塊都對應自己的動作,這里呢老魏就不在寫了。本章主要解決一下角色和動作之間的關系,由於角色和動作之間是多對多的關系,所以牽涉到了一個關系表“ArHat_Role_Action”。這張表中存儲的是角色和動作。
首先,我們創建一個RoleController控制器,然后給Index函數添加一個視圖文件Index.cshtml。內容如下:
@{ ViewBag.Title = "角色管理"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section script{ $(document).ready(function(){ initUserGrid(); }); function initUserGrid(){ $("#grid").datagrid({ url:'/Role/LoadRoleData', fitColumns:true, rownumbers:true, toolbar: [{ iconCls: 'sys-icon-add', text:"添加角色", handler:addRole }], columns:[[ {field:'RId',title:'ID',width:50,align:'center'}, {field:'RName',title:'角色名',width:200,align:'center'}, {field:'opt',title:'操作',width:200,align:'center',formatter:function(v,r,i){ return "<a href='javascript:delRole("+r.RId+")'><img src='/Content/sys_icons/user_delete.png' /></a> <a href='javascript:permitRoleAction("+r.RId+")'><img src='/Content/sys_icons/user_edit.png' /></a> "; }} ]] }); } function addRole(){ $('#dialog').dialog({ title: '添加角色', iconCls:"sys-icon-add", width: 400, height: 300, closed: false, cache: false, modal: true, onClose:function(){ $("#grid").datagrid("reload"); }, content:createIFrame("/Role/AddRole") }); } function delRole(_rid){ $.messager.confirm("提示","確認要刪除此角色嗎?",function(b){ if(b){ $.get("/Permit/Role/DelRole/",{rid:_rid},function(data){ if(data=="y"){ $("#grid").datagrid("reload"); } },"text"); } }); } function permitRoleAction(_rid){ $('#dialog').dialog({ title: '角色授權', iconCls:"sys-icon-add", width: 400, height: 300, closed: false, cache: false, modal: true, onClose:function(){ $("#grid").datagrid("reload"); }, content:createIFrame("/Role/PermitRole/"+_rid) }); } } <table id="grid" data-options="fit:true"></table> <div id="dialog" style="overflow:hidden"></div>
在上面的代碼中,把數據加載和相應的功能函數都已經寫好了,下面主要是在控制器中實現相應的數據就可以了。
當點擊“添加角色”按鈕的時候,會打開一個彈出層,在彈出層中一個頁面,也就是Role控制器下的AddRole這個動作,但是這個動作要分為兩個,一個是用來作為頁面的,一個用來處理提交傳遞過來的值。下面來看一下此時的Role控制器的代碼。
public class RoleController : Controller { private BLL.B_Permit permitBLL = new BLL.B_Permit(); public ActionResult Index() { return View(); } [HttpPost] public ActionResult LoadRoleData() { var json = new { total = permitBLL.GetAllRoleList().Count, rows = permitBLL.GetAllRoleList() }; return Json(json); } [HttpGet] public ActionResult AddRole() { return View(); } [HttpPost] public ActionResult AddRole(Model.M_ArHat_Role role) { try { permitBLL.AddRole(role); StringBuilder sb = new StringBuilder(); sb.Append(@" <script> parent.$(""#dialog"").dialog(""close""); </script> "); return Content(sb.ToString()); } catch (Exception ex) { return Content("添加失敗:" + ex.Message); } } }
大家注意到了,在AddRole方法的上面有一個Attribute來說明這個方法的請求類型,這樣可以實現在控制器中方法的重載(具體請看《一步步學習ASP.NET MVC》)。
再添加角色的時候,可以把動作也查詢出來,一起操作,但這里老魏使用的單獨操作,在每條數據的后面可以通過來給角色添加動作。
這里呢,老魏得說聲抱歉了,這個頁面做的實在是拿不出手了,本來這個頁面可以使用Tree的,但是為了說明一個問題,這里沒有使用Tree。什么問題呢,當我們給角色添加一個或幾個動作的時候,點擊保存,那么再打開這個頁面的時候,同時要選中已經添加的,還要顯示沒有添加的。同時這地方要注意的是,每次給角色添加動作的時候,首先要做一件事就是把這個角色下的動作全部刪除掉,然后在添加。我們來看一下這個方法的實現。
[HttpGet] public ActionResult PermitRole(string rid) { ViewBag.ActionList = permitBLL.GetAllActionList(); ViewBag.RoleActionList = permitBLL.GetRoleActionList(rid); ViewBag.RId = rid; return View(); } [HttpPost] public ActionResult PermitRoleAction() { string aids = Request.Form["aid"]; string rid = Request.Form["RId"]; try { permitBLL.AddRoleAction(aids,rid); StringBuilder sb = new StringBuilder(); sb.Append(@" <script> parent.$(""#dialog"").dialog(""close""); </script> "); return Content(sb.ToString()); } catch (Exception ex) { return Content("添加失敗:" + ex.Message); } }
permitBLL.AddRoleAction(aids,rid)這方法就是用來添加動作的,然后我們到BLL中去看下這個方法的實現。
public void AddRoleAction(string aids,string rid) { //刪除該角色下的所有動作 permitDAL.DeleteRoleAction(rid); //添加該角色下的動作 foreach (string aid in aids.Split(',')) { permitDAL.AddRoleAction(aid, rid); } }
大家看到了,首先刪除,然后在添加,這樣前面說的那個問題就可以解決了。由於本章牽涉到的代碼比較多,這里在文章的最后提供代碼的下載。大家可以參考一下,當然了有什么需要討論的可以留言給我。