您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在MVC中使用MvcPager實(shí)現(xiàn)分頁,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
一.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 > @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(此處可不增加,直接和異步的共用同一個(gè)):
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 > @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; } }
關(guān)于怎么在MVC中使用MvcPager實(shí)現(xiàn)分頁就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。