分页3

http://www.cnblogs.com/ChrisLee2011/p/4288194.html

页面的HTML部分如下:

< div class ="tableContainer">
< input id ="currentPage" type ="hidden" value =" @ViewData[ "currentPage"] "/>
< input id ="totalPages" type ="hidden" value =" @ViewData["totalPages" ] " />
< table class ="table table-hover table-striped">
< thead>
< tr>
< th class ="col-md-4 text-center"> 乘车码 </th >
< th class ="col-md-4 text-center"> 订单号 </th >
< th class ="col-md-4 text-center"> 订单日期 </th >
</ tr>
</ thead>
< tbody>
@foreach ( var item in Model)
{
< tr>
< td> @item.BusNo </ td>
< td> @item.OrderId </ td>
< td> @item.OrderDate </ td>
</ tr>
}
</ tbody>
</ table>
< ul id ="example"></ ul>
</ div >

页面的JavaScript部分如下:(此处需要引用插件bootstrap-paginator,具体的使用方法也在官网能看到,这里就不再详述)

@ Scripts.Render( "~/bundles/bootstrap-paginator" )
< script type ="text/javascript">
$( '#example' ).bootstrapPaginator({
currentPage: $( '#currentPage' ).val(), //当前页
totalPages: $( '#totalPages' ).val(), //总页数
bootstrapMajorVersion: 3, //兼容Bootstrap3.x版本
tooltipTitles: function (type, page) {
switch (type) {
case "first" :
return "第一页" ;
case "prev" :
return "上一页" ;
case "next" :
return "下一页" ;
case "last" :
return "最后一页" ;
case "page" :
return page;
}
return "" ;
},
onPageClicked: function (event, originalEvent, type, page) {
$.get( '/Home/GetPaginationData' , { currentPage: page, pageSize:10 }, function (view) {
$( '#tableTest' ).html(view);
});
}
});
</ script >

上面的“#tableTest”是一个div,是< div class ="tableContainer">的父节点,在父页面中占位,就是说当页面数据返回将刷新整个PartialView,后台是一个GET,如下:

public ActionResult GetPaginationData( int currentPage = 1, int pageSize = 10)
{
using (var context = new TestEntities ())
{
int count;
var q = (from a in context.Tickets
join b in context.Order on a.OrderId equals b.Id
select new TableItem
{
BusNo = a.BusNumber,
OrderId = b.Id,
OrderDate = b.OrderDateTime,
}).Pagination(currentPage, pageSize, out count);
var data = q.ToList();
ViewData[ "totalPages" ] = count / pageSize + 1; //计算分页后总的页面数
ViewData[ "currentPage" ] = currentPage; //当前页码
return PartialView("~/Views/Home/OrderList.cshtml" ,data);
}
}

 其中的Pagination扩展函数用于数据库分页,请参考我的另外一篇博文 “Entity Framework 分页处理

 
原文地址:https://www.cnblogs.com/lacey/p/5596048.html