这篇文章给大家分享的是有关ASP.NET MVC4 Razor模板如何实现分页效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、无数据提交
第一步,建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
{
//int count = db.Product.Count();
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
ViewBag.action = action;
ViewBag.controller = controller;
return PartialView();
}传入四个参数:
action:操作(要分页的视图的操作,默认为Index);
controller:控制器;
currentPage:当前页数;
pageCount:数据总页数
第二步:添加视图(PageIndex)
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
您好,当前没有数据显示!
}
else
{
if (ViewBag.CurrentPage <= 10)
{
首页|
}
else
{
首页
...
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
第 @i 页|
}
if (ViewBag.CurrentPage > 1)
{
上一页|
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
下一页
}
if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
尾 页
}
else
{
...
尾 页
}
当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
}第三步:操作的视图的控制器修改
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));
}第四步:页面调用(即最后一步)
复制代码 代码如下:
@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
一般来说,数据都是变动的。
二、有数据提交
第一步:建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndexKey(int currentPage, int pageCount)
{
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
return PartialView();
}第二步:建立分布视图
@Html.Hidden("pageIndex")
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
当前没有数据
}
else
{
if (ViewBag.CurrentPage <= 10)
{
首页|
}
else
{
首页|
...|
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
第 @i 页|
}
if (ViewBag.CurrentPage >1)
{
上一页|
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
下一页
}
if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
}
else
{
...|
尾 页
}
当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
}
第三步:修改操作视图和控制器
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));
}视图(页面调用):
@using (Html.BeginForm())
{
根据性别得到查询结果
性别: @Html.TextBox("sex")
@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
}
Example:
//数据,一个list的集合 Lists = new List (); 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 })
感谢各位的阅读!关于“ASP.NET MVC4 Razor模板如何实现分页效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!