標籤:分頁 datagrid wcf mvc json
高校平台中,後台使用EF架構,前端使用MVC結合Easyui,前端和背景連結採用WCF傳遞資料,現在來說說是如何?分頁的。
頁面:mvc+Easyui
採用部分視圖的方式調用拼接表格的部分視圖,url放入頁面載入時調用的方法(controller+方法名),例如我的controller為Template,方法名為QueryTemplateData。
<div id="ContentAreas"> @*先調用 對應的業務Controller中的表頭資料*@ @{Html.RenderAction("ShowTemplateProperties", "Template");} @*載入 資料表格 datagrid 加參數——查詢業務資料的url *@ @{Html.RenderAction("DataGrid", "ControlsPartial", new { url = "/Template/QueryTemplateData"});} </div> 效果如下:
Controller下的方法QueryTemplateData如下:
pageSize指一頁顯示多少資料,pageIndex指第幾頁,out total,傳地址,返回當前表格中所有資料條目數。
#region 查詢範本主表資料——頁面首次載入 朱丹 2014年12月21日16:26:48 /// <summary> /// 查詢範本主表資料——頁面首次載入 /// </summary> /// <returns>string</returns> public ActionResult QueryTemplateData() { //pageSize頁大小 int pageSize = int.Parse(Request.QueryString["rows"].ToString().Trim()); //pageIndex第幾頁 int pageIndex = int.Parse(Request.QueryString["page"].ToString().Trim()); int total = 0; List<ExamTemplateMainEntity> listData = new List<ExamTemplateMainEntity>(); ExamTemplateMainEntity enTemplate = new ExamTemplateMainEntity(); listData = userService.QueryTemplateMain(pageSize, pageIndex, out total); var data = new { total, rows = listData }; return Json(data, JsonRequestBehavior.AllowGet); } #endregion 值得注意的要為
var data = new { total, rows = listData }; return Json(data, JsonRequestBehavior.AllowGet); 此段代碼是調用vs中的Json方法,將資料轉換成如何Easyui表格分頁的Json串,Easyui分頁的Json串為:{"total":xx,"rows":[{...},{...}]} total為總數 rows為資料數組。
WCF和後台EF架構中的邏輯就不用我說了,只是將controller中的pageSize、pageIndex、out Total參數傳後後台,擷取資料庫中的資料返回到Controller就可以了,這樣就實現了分頁。效果如下:
總結:
其實Easyui和MVC不難,再難,也要迎難而上,有困難了才是自己進步的時候!
Easyui+EF+MVC+WCF實現分頁