高校平台中,背景使用EF架構,前端使用MVC結合Easyui,前端和背景的連結采用WCF傳遞資料,現在來說說是如何實作分頁的。
頁面:mvc+Easyui
采用部分視圖的方式調用拼接表格的部分視圖,url放入頁面加載時調用的方法(controller+方法名),例如我的controller為Template,方法名為QueryTemplateData。
[html] view plain copy
- <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,傳位址,傳回目前表格中所有資料條目數。
[csharp] view plain copy
- #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
值得注意的要為
[csharp] view plain copy
- 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就可以了,這樣就實作了分頁。效果如下: