系列目錄html
爲了符合後面更新後的重構系統,文章於2016-11-1日重寫json
EasyUI Datagrid在加載的時候會提交一些分頁的信息到後臺,咱們須要根據這些信息來進行數據分頁再次返回到前臺ide
首先要讓DataGrid支持分頁,咱們須要在JS Datagrid中加入下列幾個屬性post
OK加入後的代碼變成這樣url
$(function () {
$('#List').datagrid({
url: '/SysSample/GetList',
width: $(window).width() - 10,
methord: 'post',
height: $(window).height() - 35,
fitColumns: true,
sortName: 'Id',
sortOrder: 'desc',
idField: 'Id',
pageSize: 15,
pageList: [15, 20, 30, 40, 50],
pagination: true,
striped: true, //奇偶行是否區分
singleSelect: true,//單選模式
rownumbers: true,//行號
columns: [[
{ field: 'Id', title: 'ID', width: 80 },
{ field: 'Name', title: '名稱', width: 120 },
{ field: 'Age', title: '年齡', width: 80, align: 'right' },
{ field: 'Bir', title: '生日', width: 80, align: 'right' },
{ field: 'Photo', title: '照片', width: 250 },
{ field: 'Note', title: '說明', width: 60, align: 'center' },
{ field: 'CreateTime', title: '建立時間', width: 60, align: 'center' }
]]
});
});
預覽一下spa
實際已經分頁,可是不正確的,每一頁的數據同樣。咱們要根據分頁的參數去取code
OK咱們控制器要根據他傳遞的參數寫一樣的參數名稱,不然獲取不到htm
看咱們的SysSampleController 下的GetList方法,加入參數blog
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using App.BLL; using App.IBLL; using App.Models; using App.Models.Sys; using Microsoft.Practices.Unity; namespace App.Admin.Controllers { public class SysSampleController : Controller { // // GET: /SysSample/ /// <summary> /// 業務層注入 /// </summary> [Dependency] public ISysSampleBLL m_BLL { get; set; } public ActionResult Index() { return View(); } [HttpPost] public JsonResult GetList(int page = 1, int rows = 10, string sort = "Id", string order = "desc") { int total = 0; List<SysSampleModel> list = m_BLL.GetList(page, rows, sort, order, ref total); var json = new { total = total, rows = (from r in list select new SysSampleModel() { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); } } }
BLL代碼修改(IBLL也要修改參數 List<SysSampleModel> GetList(int page, int rows, string sort, string order,ref int total);)排序
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Practices.Unity; using App.Models; using App.Common; using App.Models.Sys; using App.IBLL; using App.IDAL; namespace App.BLL { public class SysSampleBLL : ISysSampleBLL { DBContainer db = new DBContainer(); [Dependency] public ISysSampleRepository Rep { get; set; } /// <summary> /// 獲取列表 /// </summary> /// <param name="pager">JQgrid分頁</param> /// <param name="queryStr">搜索條件</param> /// <returns>列表</returns> public List<SysSampleModel> GetList(int page, int rows, string sort, string order, ref int total) { IQueryable<SysSample> queryData = null; queryData = Rep.GetList(db); //排序 if (order == "desc") { switch (order) { case "Id": queryData = queryData.OrderByDescending(c => c.Id); break; case "Name": queryData = queryData.OrderByDescending(c => c.Name); break; default: queryData = queryData.OrderByDescending(c => c.CreateTime); break; } } else { switch (order) { case "Id": queryData = queryData.OrderBy(c => c.Id); break; case "Name": queryData = queryData.OrderBy(c => c.Name); break; default: queryData = queryData.OrderBy(c => c.CreateTime); break; } } return CreateModelList(ref queryData,page,rows,ref total); } private List<SysSampleModel> CreateModelList(ref IQueryable<SysSample> queryData,int page,int rows,ref int total) { total = queryData.Count(); if (total > 0) { if (page <= 1) { queryData = queryData.Take(rows); } else { queryData = queryData.Skip((page - 1) * rows).Take(rows); } } List<SysSampleModel> modelList = (from r in queryData select new SysSampleModel { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToList(); return modelList; } /// <summary> /// 建立一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Create( SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity != null) { return false; } entity = new SysSample(); entity.Id = model.Id; entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; entity.CreateTime = model.CreateTime; if (Rep.Create(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 刪除一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="id">id</param> /// <returns>是否成功</returns> public bool Delete(string id) { try { if (Rep.Delete(id) == 1) { return true; } else { return false; } } catch (Exception ex) { return false; } } /// <summary> /// 修改一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Edit(SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity == null) { return false; } entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; if (Rep.Edit(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 判斷是否存在實體 /// </summary> /// <param name="id">主鍵ID</param> /// <returns>是否存在</returns> public bool IsExists(string id) { if (db.SysSample.SingleOrDefault(a => a.Id == id) != null) { return true; } return false; } /// <summary> /// 根據ID得到一個實體 /// </summary> /// <param name="id">id</param> /// <returns>實體</returns> public SysSampleModel GetById(string id) { if (IsExist(id)) { SysSample entity = Rep.GetById(id); SysSampleModel model = new SysSampleModel(); model.Id = entity.Id; model.Name = entity.Name; model.Age = entity.Age; model.Bir = entity.Bir; model.Photo = entity.Photo; model.Note = entity.Note; model.CreateTime = entity.CreateTime; return model; } else { return new SysSampleModel(); } } /// <summary> /// 判斷一個實體是否存在 /// </summary> /// <param name="id">id</param> /// <returns>是否存在 true or false</returns> public bool IsExist(string id) { return Rep.IsExist(id); } } }
咱們要在BLL層返回當前查詢的所有條數,還要返回當前頁得數據
補腦:ref關鍵字使參數按引用傳遞。其效果是,當控制權傳遞迴調用方法時,在方法中對參數所作的任何更改都將反映在該變量中。若要使用ref參數,則方法定義和調用方法都必須顯式使用ref關鍵字。
你們看出這樣太麻煩了,代碼太不漂亮了,咱們把經常使用的參數給封裝了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace App.Common { public class GridPager { public int rows { get; set; }//每頁行數 public int page { get; set; }//當前頁是第幾頁 public string order { get; set; }//排序方式 public string sord { get; set; }//排序列 public int totalRows { get; set; }//總行數 public int totalPages //總頁數 { get { return (int)Math.Ceiling((float)totalRows / (float)rows); } } } }
把這個類放到Apps.Common, 咱們的Apps.Common終於用到了
再次修改Controller的GetList
[HttpPost] public JsonResult GetList(GridPager pager) { List<SysSampleModel> list = m_BLL.GetList(ref pager); var json = new { total = pager.totalRows, rows = (from r in list select new SysSampleModel() { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); }
和修改BLL的GetList 還要修改IBLL --List<SysSampleModel> GetList(ref GridPager pager);
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Practices.Unity; using App.Models; using App.Common; using App.Models.Sys; using App.IBLL; using App.IDAL; namespace App.BLL { public class SysSampleBLL : ISysSampleBLL { DBContainer db = new DBContainer(); [Dependency] public ISysSampleRepository Rep { get; set; } /// <summary> /// 獲取列表 /// </summary> /// <param name="pager">JQgrid分頁</param> /// <param name="queryStr">搜索條件</param> /// <returns>列表</returns> public List<SysSampleModel> GetList(ref GridPager pager) { IQueryable<SysSample> queryData = null; queryData = Rep.GetList(db); //排序 if (pager.order == "desc") { switch (pager.order) { case "CreateTime": queryData = queryData.OrderByDescending(c => c.CreateTime); break; case "Name": queryData = queryData.OrderByDescending(c => c.Name); break; default: queryData = queryData.OrderByDescending(c => c.CreateTime); break; } } else { switch (pager.order) { case "CreateTime": queryData = queryData.OrderBy(c => c.CreateTime); break; case "Name": queryData = queryData.OrderBy(c => c.Name); break; default: queryData = queryData.OrderBy(c => c.CreateTime); break; } } return CreateModelList(ref pager, ref queryData); } private List<SysSampleModel> CreateModelList(ref GridPager pager, ref IQueryable<SysSample> queryData) { pager.totalRows = queryData.Count(); if (pager.totalRows > 0) { if (pager.page <= 1) { queryData = queryData.Take(pager.rows); } else { queryData = queryData.Skip((pager.page - 1) * pager.rows).Take(pager.rows); } } List<SysSampleModel> modelList = (from r in queryData select new SysSampleModel { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToList(); return modelList; } /// <summary> /// 建立一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Create( SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity != null) { return false; } entity = new SysSample(); entity.Id = model.Id; entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; entity.CreateTime = model.CreateTime; if (Rep.Create(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 刪除一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="id">id</param> /// <returns>是否成功</returns> public bool Delete(string id) { try { if (Rep.Delete(id) == 1) { return true; } else { return false; } } catch (Exception ex) { return false; } } /// <summary> /// 修改一個實體 /// </summary> /// <param name="errors">持久的錯誤信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Edit(SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity == null) { return false; } entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; if (Rep.Edit(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 判斷是否存在實體 /// </summary> /// <param name="id">主鍵ID</param> /// <returns>是否存在</returns> public bool IsExists(string id) { if (db.SysSample.SingleOrDefault(a => a.Id == id) != null) { return true; } return false; } /// <summary> /// 根據ID得到一個實體 /// </summary> /// <param name="id">id</param> /// <returns>實體</returns> public SysSampleModel GetById(string id) { if (IsExist(id)) { SysSample entity = Rep.GetById(id); SysSampleModel model = new SysSampleModel(); model.Id = entity.Id; model.Name = entity.Name; model.Age = entity.Age; model.Bir = entity.Bir; model.Photo = entity.Photo; model.Note = entity.Note; model.CreateTime = entity.CreateTime; return model; } else { return new SysSampleModel(); } } /// <summary> /// 判斷一個實體是否存在 /// </summary> /// <param name="id">id</param> /// <returns>是否存在 true or false</returns> public bool IsExist(string id) { return Rep.IsExist(id); } } }
代碼漂亮了很多,預覽一下,仍是正確的效果了。
文章中一直演示作法,這種方式看起來很是反感,可是都是爲了更好更仔細的瞭解系統,爲後面快速開發作鋪墊