ASP.NET MVC4 Razor模板簡易分頁效果
一、無數(shù)據(jù)提交
第一步,建立一個 Controller命名為PageIndex的空控制器,自定義一個方法如下:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
{
//int count = db.Product.Count();
ViewBag.PageCount = pageCount;//從操作中獲取總數(shù)據(jù)頁數(shù)將傳入分頁視圖頁面
ViewBag.CurrentPage = currentPage;//從操作中獲取當(dāng)前頁數(shù)將傳入分頁視圖頁面
ViewBag.action = action;
ViewBag.controller = controller;
return PartialView();
}
傳入四個參數(shù):
action:操作(要分頁的視圖的操作,默認(rèn)為Index);
controller:控制器;
currentPage:當(dāng)前頁數(shù);
pageCount:數(shù)據(jù)總頁數(shù)
第二步:添加視圖(PageIndex)
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>您好,當(dāng)前沒有數(shù)據(jù)顯示!</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首頁</a>|</span>
}
else
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首頁</a>
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">
...</a> </span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">
第 @i 頁</a>|</span>
}
if (ViewBag.CurrentPage > 1)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">
上一頁</a>|</span>
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">
下一頁</a></span>
}
if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 頁</a>
}
else
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">
...</a></span>
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 頁</a>
}
<span style="padding-left: 20px">當(dāng)前頁數(shù): @ViewBag.CurrentPage | 共 @ViewBag.PageCount 頁
</span>
}
第三步:操作的視圖的控制器修改
public ViewResult Index(int? pageIndex)
{
int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);
//這里的是take,按照每頁20個顯示
return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
}
第四步:頁面調(diào)用(即最后一步)
一般來說,數(shù)據(jù)都是變動的。
二、有數(shù)據(jù)提交
第一步:建立一個 Controller命名為PageIndex的空控制器,自定義一個方法如下:
public ActionResult PageIndexKey(int currentPage, int pageCount)
{
ViewBag.PageCount = pageCount;//從操作中獲取總數(shù)據(jù)頁數(shù)將傳入分頁視圖頁面
ViewBag.CurrentPage = currentPage;//從操作中獲取當(dāng)前頁數(shù)將傳入分頁視圖頁面
return PartialView();
}
第二步:建立分布視圖
<script>
$(function () {
$("#pageingByForm a").click(function (event) {
$("#pageIndex").val($(this).attr("pageIndex"));
//$(this).parent("Form").submit();
document.getElementsByTagName("Form").item(0).submit();
event.preventDefault();
});
});
</script>
@Html.Hidden("pageIndex")
<div id="pageingByForm">
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>當(dāng)前沒有數(shù)據(jù)</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a pageindex="1" href="#">首頁</a>|</span>
}
else
{
<span><a pageindex="1" href="#">首頁</a>|</span>
<span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a pageIndex="@i" href="#">第 @i 頁</a>|</span>
}
if (ViewBag.CurrentPage >1)
{
<span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">上一頁</a>|</span>
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
<span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">下一頁</a></span>
}
if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
}
else
{
<span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>
<span><a pageIndex="@ViewBag.PageCount" href="#">尾 頁</a></span>
}
<span style="padding-left: 20px">當(dāng)前頁數(shù): @ViewBag.CurrentPage | 共 @ViewBag.PageCount 頁
</span>
}
</div>
第三步:修改操作視圖和控制器
public ViewResult Index(int? pageIndex ,string search)
{
int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);
return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
}
視圖(頁面調(diào)用):
@using (Html.BeginForm())
{
根據(jù)性別得到查詢結(jié)果
性別: @Html.TextBox("sex")
<input type="submit" value="查詢" />
@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
}
Example:
//數(shù)據(jù),一個list的集合
List<string> s = new List<string>();
s.Add("張軍");
ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0);
return View(s.Skip((pageInd - 1) * 20).Take(20));
@Html.Action("PageIndex", "PageIndex",
new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁教程(PagedList.Mvc)
- MVC+jQuery.Ajax異步實現(xiàn)增刪改查和分頁
- MVC分頁之MvcPager使用詳解
- ASP.NET MVC分頁的實現(xiàn)方法
- ASP.NET MVC分頁和排序功能實現(xiàn)
- ASP.NET MVC5 實現(xiàn)分頁查詢的示例代碼
- ASP.NET MVC4 HtmlHelper擴(kuò)展類,實現(xiàn)分頁功能
- Asp.net MVC 中利用jquery datatables 實現(xiàn)數(shù)據(jù)分頁顯示功能
- ASP.NET MVC+EF在服務(wù)端分頁使用jqGrid以及jquery Datatables的注意事項
- MVC使用MvcPager實現(xiàn)分頁效果
相關(guān)文章
ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡易的方法
ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡易的方法...2006-09-09
vs.net 2010 擴(kuò)展插件小結(jié) 提高編程效率
本文價紹了幾款Visual Studio提供的插件,提高我們的編程效率。2011-03-03
ASP.NET堆和棧二之值類型和引用類型的參數(shù)傳遞和內(nèi)存分配
這篇文章介紹了ASP.NET堆和棧中值類型和引用類型的參數(shù)傳遞和內(nèi)存分配,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
asp.net計算一串?dāng)?shù)字中每個數(shù)字出現(xiàn)的次數(shù)
計算一串?dāng)?shù)字中每個數(shù)字出現(xiàn)的次數(shù),可以這樣子,先判斷輸入的字符串是不是數(shù)字組成,還是否包含有其它字符2012-05-05
asp.net IList查詢數(shù)據(jù)后格式化數(shù)據(jù)再綁定控件
這篇文章送給.net初學(xué)者或者遇到類似問題的朋友,就是IList如何格式化數(shù)據(jù)再綁定,我看到網(wǎng)上沒有多少朋友講到這方面的最基本的問題,現(xiàn)在我簡單說說吧,代碼我就截取其中一些講,如果不明白的朋友可以留言或者聯(lián)系我。2009-11-11

