mvc4 html.pager,MVC分页之MvcPager使用详解
最近剛剛接觸MVC不久,因項目中要用到分頁,網上找了下資料,最后采用了MvcPager(http://www.webdiyer.com/),支持同步和Ajax異步分頁。廢話不多說了直接上代碼。
一.MvcPager異步?ViewModel:
public class Article
{
[Display(Name = "信息編號")]
public int ID { get; set; }
[Display(Name = "信息標題")]
public string Title { get; set; }
[Display(Name = "信息內容")]
public string Content { get; set; }
}
public class AjaxPager
{
public PagedList Articles { get; set; }
}
Control:
///
/// 異步分頁測試
///
/// pageIndex
/// 關鍵字
///
public ActionResult AjaxPaging(int? id = 1, string key = null)
{
int totalCount = 0;
int pageIndex = id ?? 1;
int pageSize = 2;
List infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount);
PagedList 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;
MVCPager-AjaxPaging@using (Html.BeginForm("AjaxPaging", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get))
{
@Html.Label("關鍵字:")
}
@*分頁Table*@
@{ Html.RenderPartial("_ArticleTable"); }
@Ajax.Pager(Model.Articles, new PagerOptions
{
PageIndexParameterName = "id",
FirstPageText = "首頁",
PrevPageText = "上一頁",
NextPageText = "下一頁",
LastPageText = "末頁",
NumericPagerItemCount = 5,
ContainerTagName = "ul",
CssClass = "pagination",
CurrentPagerItemTemplate = "
{0}",DisabledPagerItemTemplate = "
{0}",PagerItemTemplate = "
{0}"}).AjaxOptions(a => a.SetUpdateTargetId("articles"))
@model soulefu_manage.Models.MyTest.AjaxPager
@foreach (var item in Model.Articles)
{
@Html.DisplayFor(model => item.ID)@Html.DisplayFor(modelItem => item.Title)
@Html.DisplayFor(modelItem => item.Content)
}
二.MvcPager同步
ViewModel(此處可不增加,直接和異步的共用同一個):
public class MVCPager
{
//信息列表
public PagedList Articles { get; set; }
}
Control:
///
/// 同步分頁測試
///
/// pageIndex
/// 關鍵字
///
public ActionResult MVCPager(int? id = 1, string key = null)
{
int totalCount = 0;
int pageIndex = id ?? 1;
int pageSize = 2;
List infoList = new SoleFuDAL.MyTest().GetArticleList(key, pageSize, (pageIndex - 1) * 2, out totalCount);
PagedList InfoPager = infoList.AsQueryable().OrderByDescending(o => o.ID).ToPagedList(pageIndex, pageSize);
InfoPager.TotalItemCount = totalCount;
InfoPager.CurrentPageIndex = (int)(id ?? 1);
//數據組裝到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;
MVCPager@using (Html.BeginForm("MVCPager", "MyTest", new RouteValueDictionary { { "id", "" } }, FormMethod.Get))
{
@Html.Label("關鍵字:")
}
@foreach (var info in Model.Articles)
{
@Html.DisplayFor(model => info.ID)@Html.DisplayFor(model => info.Title)@Html.DisplayFor(model => info.Content)}
@Html.Pager(Model.Articles, new PagerOptions
{
PageIndexParameterName = "id",
FirstPageText = "首頁",
PrevPageText = "上一頁",
NextPageText = "下一頁",
LastPageText = "末頁",
ContainerTagName = "ul",
CssClass = "pagination",
CurrentPagerItemTemplate = "
{0}",DisabledPagerItemTemplate = "
{0}",PagerItemTemplate = "
{0}",Id = "bootstrappager"
})
獲取測試數據方法(共用):
public class MyTest
{
///
/// 獲取測試數據
///
///
///
///
///
///
public List GetArticleList(string key, int PageSize, int CurrentCount, out int TotalCount)
{
string tabName = string.Format("Article");
string strWhere = " 1=1";
if (!string.IsNullOrEmpty(key))
{
//SQL關鍵字過濾 包含關鍵字則不拼接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 list = new List();
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)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持找一找教程網。
總結
以上是生活随笔為你收集整理的mvc4 html.pager,MVC分页之MvcPager使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]Eclipse Java注释模板设
- 下一篇: 微PE工具箱介绍