Asp.net MVC3表格共用分页功能

在建立的mvc3项目中,在Razor(CSHTML)视图引擎下,数据会在表格中自动的生成,但分页没有好的控件实现,这里我们开发了设计了一个分页的模板,适合于没有数据提交和有数据提交的分页的分页。

第一:没有有数据提交的分页功能:

第一步,建立一个 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();

}

传入四个参数,分别为:操作,控制器,当前页数,数据总页数,操作是指你要分页的试图的操作一般为Index,控制器为该试图对应的控制器, 这四个都为视图传递数据的。

第二步:在PageIndex控制器下的Index上添加视图选择分布视图,代码如下:

@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)

{

<span>您好,当前没有数据显示!</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 >当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页

</span>

} 在操纵中(Index)的部分代码: public ViewResult Index(int? pageIndex) { 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)); } 这个意思是以二十也一页作为当前页. 在你要分页的页面的下面加入调用: @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();

} 第二步:在要分页的页面Index上添加视图选择分布视图,代码如下 <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 >

@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)

{

<span>当前没有数据</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 >当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页

</span>

}

</div>

在操纵中(Index)的部分代码:

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") 这个是指按性别进行过分页 <input type="submit" value="查询" /> 最后调用@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })} 其他的和第一中相符 。

示例:

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 = "Index", controller = "LogController", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })