在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
接下来先做角色这一板块的(增删改查),首先要新建一个Role控制器,在添加一个RoleList的视图。表格打算采用的是bootstrap的表格。 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthorDesign.Web.Areas.Admin.Controllers { public class RoleController : Controller { // // GET: /Admin/Role/ public ActionResult RoleList() { return View(); } } } 在当我做查询的时候返现角色少了个排序,也可以说是角色的等级吧,也可以就是用来排序用的。那么老方法先在AuthorDesign.Model.Authory这个类下面新增一个属性 /// <summary> /// 排序数字 /// </summary> public int OrderNum { get; set; } 然在打开控制台选择DAL然后输入add-migration AddAuthoryOrderNum 完成之后再次输入update-database即可。 然后在Model下建立一个ParamModel,这是专门用来接收分页信息的。 #region 分页基类 /// <summary> /// 分页基类 /// </summary> public class ParamModel { /// <summary> /// DataTable请求服务器端次数 /// </summary> public string sEcho { get; set; } /// <summary> /// 每页显示的数量 /// </summary> public int iDisplayLength { get; set; } /// <summary> /// 分页时每页跨度数量 /// </summary> public int iDisplayStart { get; set; } } #endregion 然后在建立一个RoleParamModel,继承下ParamModel, #region 角色分页 /// <summary> /// 角色分页 /// </summary> public class RoleParamModel : ParamModel { /// <summary> /// 是否根据排序字段进行倒序排列 /// </summary> public bool IsDesc { get; set; } } #endregion 接下在AuthoryRepository这个下面写一个获取权限分页的方法 /// <summary> /// 获取角色分页列表 /// </summary> /// <param name="startNum">起始数字</param> /// <param name="pageSize">页长</param> /// <param name="isDesc">是否倒序排列</param> /// <param name="rowCount">总页数</param> /// <returns>角色分页列表</returns> public IQueryable<Authory> LoadPageList(int startNum, int pageSize, bool isDesc, out int rowCount) { rowCount = 0; var result = from p in db.Set<Authory>() select p; if (isDesc) { result = result.OrderByDescending(m => m.OrderNum); } else { result = result.OrderBy(m => m.OrderNum); } rowCount = result.Count(); return result.Skip(startNum).Take(pageSize); } 我可能实现的方法写的不好,如果有什么地方需要改进的麻烦你们告诉我下。 然后再去IAuthoryRepository这个接口上定义下规范 /// <summary> /// 获取角色分页列表 /// </summary> /// <param name="startNum">起始数字</param> /// <param name="pageSize">页长</param> /// <param name="isDesc">是否倒序排列</param> /// <param name="rowCount">总页数</param> /// <returns>角色分页列表</returns> IQueryable<Authory> LoadPageList(int startNum, int pageSize, bool isDesc, out int rowCount); 接下来就是前台的调用了,在Role控制下添加一个返回JsonResult的方法 public ActionResult RoleList() { return View(); } [HttpPost] public JsonResult RoleList(Models.RoleParamModel model) { int rowCount = 0; var result = EnterRepository.GetRepositoryEnter().GetAuthoryRepository.LoadPageList(model.iDisplayStart, model.iDisplayLength, model.IsDesc, out rowCount); return Json(new { sEcho = model.sEcho, iTotalRecords = rowCount, iTotalDisplayRecords = rowCount, aaData = result }, JsonRequestBehavior.AllowGet); } 先对RoleParamModel这个类解释下,这类里面有四个属性,分别是 /// <summary> /// DataTable请求服务器端次数 /// </summary> public string sEcho { get; set; } /// <summary> /// 每页显示的数量 /// </summary> public int iDisplayLength { get; set; } /// <summary> /// 分页时每页跨度数量 /// </summary> public int iDisplayStart { get; set; } /// <summary> /// 是否根据排序字段进行倒序排列 /// </summary> public bool IsDesc { get; set; } sEcho是jquery.dataTables这个插件在与后台进行交互的时候会自动传过来的(aData里面可以获取到的);iDisplayLength与iDisplayStart也是同样可以获取的到的,然后IsDesc这个字段用来表示用户是选择按照排序字段正序排序还是倒序排序用的。下面给上我的前台代码: @section Header{ <style type="text/css"> /*加载动态图片*/ .dataTables_processing { position: absolute; top: 50%; left: 50%; width: 100%; height: 40px; margin-left: -50%; margin-top: -25px; padding-top: 20px; text-align: center; font-size: 1.2em; /*background-color: white;*/ } </style> } <div class="main-content"> <div class="breadcrumbs" id="breadcrumbs"> <script type="text/javascript"> try { ace.settings.check('breadcrumbs', 'fixed') } catch (e) { } </script> <ul class="breadcrumb"> <li> <i class="icon-home home-icon"></i> <a href="/Admin/Home">首页</a> </li> <li> <a href="/Admin/Role/RoleList">角色列表</a> </li> <li class="active">角色列表</li> </ul><!-- .breadcrumb --> <!-- #nav-search --> </div> <div class="page-content"> <div class="row"> <div class="col-xs-12"> <!-- PAGE CONTENT BEGINS --> <div class="row"> <div class="col-xs-12"> <div class="table-header"> 角色列表查看 </div> <div class="table-responsive dataTables_wrapper"> <table id="sample-table-2" class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="center sorting_disabled" role="columnheader" rowspan="1" colspan="1" aria-label="" style="width: 58px;"> <label> <input type="checkbox" class="ace"> <span class="lbl"></span> </label> </th> <th>角色名称</th> <th>角色简介</th> <th>角色状态</th> <th>角色排序</th> <th style="width: 120px;"> 操作 </th> </tr> </thead> </table> </div> </div> </div> <!-- PAGE CONTENT ENDS --> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.page-content --> </div> @section script{ <script src="/Content/assets/js/jquery.dataTables.min.js"></script> <script src="/Content/assets/js/jquery.dataTables.bootstrap.js"></script> <script src="/Content/assets/js/bootbox.min.js"></script> <script type="text/javascript"> var isSearch = false; $(function () { var objTable = $("#sample-table-2").dataTable({ aoColumns: [ { "sClass": "center", "mDataProp": "Id", "bSortable": false }, { "sClass": "center", "mDataProp": "Name", "bSortable": false }, { "sClass": "center", "mDataProp": "Intro", "bSortable": false }, { "sClass": "center", "mDataProp": "State", "bSortable": false }, { "mDataProp": "OrderNum", "sClass": "center", "bSortable": true, "asSorting": ["asc", "desc"] }, { "sClass": "center", "mDataProp": "Id", "bSortable": false } ], "bServerSide": true,//分页,取数据等等的都放到服务端去 "bProcessing": true,//载入数据的时候是否显示“载入中” "aLengthMenu": [30, 50, 100], "bLengthChange": true, //改变每页显示数据数量 //"bFilter": false, //过滤功能 "iDisplayStart": 0, "iDisplayLength": 30,//首次加载的数据条数 "bStorable": true,//排序操作在服务端进行,所以可以关了。 "sAjaxSource": "/Admin/Role/RoleList", "fnServerParams": function (aoData, bStorable) { }, /*如果加上下面这段内容,则使用post方式传递数据*/ "fnServerData": function (sSource, aoData, fnCallback, bStorable) { var paramList = { "sEcho": 0, "iDisplayLength": 0, "iDisplayStart": 0, "isDesc": bStorable.aaSorting[0][1] == "desc" ? true : false }; if (bStorable.aaSorting[0][0] == 0) { paramList.isDesc = true; } for (var i = 0; i < aoData.length; i++) { if (aoData[i].name == "iDisplayStart") { paramList.iDisplayStart = aoData[i].value; } else if (aoData[i].name == "iDisplayLength") { paramList.iDisplayLength = aoData[i].value; } else if (aoData[i].name == "sEcho") { paramList.sEcho = aoData[i].value; } } $.ajax({ "dataType": 'json', "type": "POST", "url": sSource, "data": paramList, "success": function (resp) { fnCallback(resp); //服务器端返回的对象的returnObject部分是要求的格式 } }); }, "oLanguage": { "sSearch": "搜索", "sLengthMenu": "每页显示 _MENU_ 条记录", "sZeroRecords": "抱歉, 没有找到", "sInfo": "从 _START_ 到 _END_ 共 _TOTAL_ 条数据", "sInfoEmpty": "没有数据", "sInfoFiltered": "(从 _MAX_ 条数据中检索)", "oPaginate": { "sFirst": "首页", "sPrevious": "前一页", "sNext": "后一页", "sLast": "尾页" }, "sZeroRecords": "没有检索到数据", "sProcessing": "<img src='/Content/assets/images/loading.gif' />" }, "fnRowCallback": function (nRow, aData, iDisplayIndex) { /* 用来改写指定行的样式 */ $('td:eq(0)', nRow).html("<label><input type=\"checkbox\" class=\"ace\" value=\"" + aData.Id + "\"><span class=\"lbl\"></span></label>"); return nRow; } }); $("#sample-table-2_filter").html("");//移除自带的搜索 }) </script> } 前台代码主要是jquery.dataTables的使用,我对jquery.dataTables获取那个特定字段的排序还不是很清楚,只能通过遍历下bStorable里面的内容去获取。下面稍微解释下前台代码 "sAjaxSource": "/Admin/Role/RoleList",这个主要定义下服务请求的路径。aoColumns:这下面主要是设置列的信息与样式用的。 "fnRowCallback": function (nRow, aData, iDisplayIndex) { /* 用来改写指定行的样式 */ $('td:eq(0)', nRow).html("<label><input type=\"checkbox\" class=\"ace\" value=\"" + aData.Id + "\"><span class=\"lbl\"></span></label>"); return nRow; } 这代码主要用来改写指定行的样式,比如第0行就把他改为了选择框的样式。 这里是jquery-datatables的一些信息,可以自己去看下。$("#sample-table-2_filter").html("");//移除自带的搜索 这段代码主要是为了移除掉原来他自带的搜索框,你可以先去掉看下效果然后再加上。下面放上页面的显示效果 那么做了列表之后接下来就是对权限的增加与修改了。下面是Role控制器的全部代码,里面包括了修改,删除,增加,更改角色状态 using AuthorDesign.Web.App_Start.Common; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthorDesign.Web.Areas.Admin.Controllers { public class RoleController : Controller { // // GET: /Admin/Role/ public ActionResult RoleList() { return View(); } [HttpPost] public JsonResult RoleList(Models.RoleParamModel model) { int rowCount = 0; var result = EnterRepository.GetRepositoryEnter().GetAuthoryRepository.LoadPageList(model.iDisplayStart, model.iDisplayLength, model.IsDesc, out rowCount); return Json(new { sEcho = model.sEcho, iTotalRecords = rowCount, iTotalDisplayRecords = rowCount, aaData = result }, JsonRequestBehavior.AllowGet); } /// <summary> /// 获取角色信息 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpPost] public JsonResult GetRoleInfo(int id = 0) { var result = EnterRepository.GetRepositoryEnter().GetAuthoryRepository.LoadEntities(m => m.Id == id).FirstOrDefault(); if (result == null) { return Json(new { state = "error", message = "角色不存在" }); } else { return Json(new { state = "success", result }); } } /// <summary> /// 添加角色 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public JsonResult AddRole(Models.RoleModel model) { if (ModelState.IsValid) { IDAL.IAuthoryRepository authoryRepository = EnterRepository.GetRepositoryEnter().GetAuthoryRepository; //判断权限名称是否已存在 var result = authoryRepository.LoadEntities(m => m.Name == model.Name.Trim()).FirstOrDefault(); if (result == null) { authoryRepository.AddEntity(new Model.Authory() { Intro = model.Intro, Name = model.Name, OrderNum = model.OrderNum, State = model.State }); //添加下操作记录 PublicFunction.AddOperation(1, string.Format("添加角色"), string.Format("添加角色=={0}==成功", model.Name)); if (EnterRepository.GetRepositoryEnter().SaveChange() > 0) { return Json(new { state = "success", message = "添加角色成功" }); } else { PublicFunction.AddOperation(1, string.Format("添加角色"), string.Format("添加角色=={0}==失败", model.Name)); EnterRepository.GetRepositoryEnter().SaveChange(); return Json(new { state = "error", message = "添加角色失败" }); } } else { return Json(new { state = "error", message = "角色名称已经存在了" }); } } else { return Json(new { state = "error 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论