MVC分頁之MvcPager使用詳解
最近剛剛接觸MVC不久,因項(xiàng)目中要用到分頁,網(wǎng)上找了下資料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax異步分頁。廢話不多說了直接上代碼。
一.MvcPager異步
ViewModel:
public class Article { [Display(Name = "信息編號")] public int ID { get; set; } [Display(Name = "信息標(biāo)題")] public string Title { get; set; } [Display(Name = "信息內(nèi)容")] public string Content { get; set; } }
public class AjaxPager { public PagedList<Article> Articles { get; set; } }
Control:
/// <summary> /// 異步分頁測試 /// </summary> /// <param name="id">pageIndex</param> /// <param name="key">關(guān)鍵字</param> /// <returns></returns> public ActionResult AjaxPaging(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize); InfoPager.TotalItemCount = totalCount; InfoPager.CurrentPageIndex = (int)(id ?? 1); Models.MyTest.AjaxPager model = new Models.MyTest.AjaxPager(); model.Articles = InfoPager; if (Request.IsAjaxRequest()) { return PartialView("_ArticleList", model); } return View(model); }
View:
@model soulefu_manage.Models.MyTest.AjaxPager @using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>MVCPager-AjaxPaging</title> <link href="~/Content/pagerstyles.css" rel="stylesheet" /> <link href="~/Content/bootstrap.css" rel="stylesheet" /> </head> <body> <div style="padding: 15px;"> @using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("關(guān)鍵字:") <input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查詢" /> } @*分頁Table*@ @{ Html.RenderPartial("_ArticleTable"); } <div class="text-center"> @Ajax.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首頁", PrevPageText = "上一頁", NextPageText = "下一頁", LastPageText = "末頁", NumericPagerItemCount = 5, ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("articles")) </div> </div> </body> </html>
@model soulefu_manage.Models.MyTest.AjaxPager <table class="table table-bordered table-striped"> <tr> <th class="nowrap">序號</th> <th> 標(biāo)題 </th> <th> 內(nèi)容 </th> </tr> @foreach (var item in Model.Articles) { <tr> <td>@Html.DisplayFor(model => item.ID)</td> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Content) </td> </tr> } </table>
二.MvcPager同步
ViewModel(此處可不增加,直接和異步的共用同一個):
public class MVCPager { //信息列表 public PagedList<Article> Articles { get; set; } }
Control:
/// <summary> /// 同步分頁測試 /// </summary> /// <param name="id">pageIndex</param> /// <param name="key">關(guān)鍵字</param> /// <returns></returns> public ActionResult MVCPager(int? id = 1, string key = null) { int totalCount = 0; int pageIndex = id ?? 1; int pageSize = 2; List<Article> infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount); PagedList<Article> InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize); InfoPager.TotalItemCount = totalCount; InfoPager.CurrentPageIndex = (int)(id ?? 1); //數(shù)據(jù)組裝到viewModel Models.MyTest.MVCPager model = new Models.MyTest.MVCPager(); model.Articles = InfoPager; return View(model); }
View:
@model soulefu_manage.Models.MyTest.MVCPager @using Webdiyer.WebControls.Mvc; <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>MVCPager</title> <link href="~/Content/pagerstyles.css" rel="stylesheet" /> <link href="~/Content/bootstrap.css" rel="stylesheet" /> </head> <body> <div style="padding:15px;"> @using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get)) { @Html.Label("關(guān)鍵字:")<input name="key" value="@Request.QueryString["key"]" /><input type="submit" value="查詢" /> } <table class="table table-bordered table-striped"> <tr> <th>編號</th> <th>標(biāo)題</th> <th>內(nèi)容</th> </tr> @foreach (var info in Model.Articles) { <tr> <td>@Html.DisplayFor(model => info.ID)</td> <td>@Html.DisplayFor(model => info.Title)</td> <td>@Html.DisplayFor(model => info.Content)</td> </tr> } </table> <div class="text-center"> <nav> @Html.Pager(Model.Articles, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首頁", PrevPageText = "上一頁", NextPageText = "下一頁", LastPageText = "末頁", ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>", Id = "bootstrappager" }) </nav> </div> </div> </body> </html>
獲取測試數(shù)據(jù)方法(共用):
public class MyTest { /// <summary> /// 獲取測試數(shù)據(jù) /// </summary> /// <param name="key"></param> /// <param name="PageSize"></param> /// <param name="CurrentCount"></param> /// <param name="TotalCount"></param> /// <returns></returns> public List<Article> GetArticleList(string key, int PageSize, int CurrentCount, out int TotalCount) { string tabName = string.Format("Article"); string strWhere = " 1=1"; if (!string.IsNullOrEmpty(key)) { //SQL關(guān)鍵字過濾 包含關(guān)鍵字則不拼接SQL if (!SqlInjection.GetString(key)) { strWhere += string.Format(" AND (Title LIKE '%{0}%' OR Content LIKE '%{0}%')", key); } } string Order = string.Format("ID ASC"); DataSet ds = SqlHelper.GetList(SqlHelper.connStr, Order, PageSize, CurrentCount, tabName, strWhere, out TotalCount); List<Article> list = new List<Article>(); if (ds != null && ds.Tables.Count > 0) { foreach (DataRow dr in ds.Tables[0].Rows) { Article model = new Article(); model.ID = Convert.ToInt32(dr["ID"]); model.Title = dr["Title"].ToString(); model.Content = dr["Content"].ToString(); list.Add(model); } } return list; } }
效果圖:(需要引用CSS)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁教程(PagedList.Mvc)
- MVC+jQuery.Ajax異步實(shí)現(xiàn)增刪改查和分頁
- ASP.NET MVC分頁的實(shí)現(xiàn)方法
- ASP.NET MVC分頁和排序功能實(shí)現(xiàn)
- ASP.NET MVC5 實(shí)現(xiàn)分頁查詢的示例代碼
- ASP.NET MVC4 HtmlHelper擴(kuò)展類,實(shí)現(xiàn)分頁功能
- Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁顯示功能
- ASP.NET MVC4 Razor模板簡易分頁效果
- ASP.NET MVC+EF在服務(wù)端分頁使用jqGrid以及jquery Datatables的注意事項(xiàng)
- MVC使用MvcPager實(shí)現(xiàn)分頁效果
相關(guān)文章
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤之查看文件夾實(shí)現(xiàn)代碼
下面要介紹的實(shí)例包括網(wǎng)上硬盤的許多功能,將一步步為大家進(jìn)行介紹。首先創(chuàng)建工程實(shí)例,然后進(jìn)行主界面的設(shè)計(jì),最后對各個功能的實(shí)現(xiàn)分別進(jìn)行介紹2012-10-10在asp.net中實(shí)現(xiàn)datagrid checkbox 全選的方法
在asp.net中實(shí)現(xiàn)datagrid checkbox 全選的方法...2006-12-12asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中
asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中的實(shí)現(xiàn)代碼。2009-05-05ASP.NET Core使用HttpClient調(diào)用WebService
這篇文章介紹了ASP.NET Core使用HttpClient調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03DataList 中動態(tài)綁定服務(wù)器子控件的代碼
DataList 中動態(tài)綁定服務(wù)器子控件的代碼...2007-09-09ASP.NET中實(shí)現(xiàn)中文簡/繁體自動轉(zhuǎn)換的類
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)中文簡/繁體自動轉(zhuǎn)換的類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2006-09-09