Поскольку https://github.com/TroyGoode/PagedList больше не поддерживается, я предлагаю вам попробовать https://github.com/kpi-ua/X.PagedList, который является форком того же проекта, но главное отличие в том, что X.PagedList является переносимой сборкой.Это означает, что вы можете использовать его не только в веб-проектах, но и в проектах Winforms, Window Phone, Silverlight и т. Д. "
Чтобы установить его с помощью консоли диспетчера пакетов Nuget, выполните эту команду
PM> Install-Package X.PagedList.Mvc
ВАЖНО: при запуске предыдущей команды будет пытаться установить JQuery 2.0 и другие зависимости, созданные Microsoft
, и выполнить действия, указанные в https://github.com/kpi-ua/X.PagedList
/ Controllers / ProductController.cs
public class ProductController : Controller
{
public object Index(int? page)
{
var products = MyProductDataSource.FindAllProducts(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?
var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize
ViewBag.OnePageOfProducts = onePageOfProducts;
return View();
}
}
/ Views / Products / Index.cshtml
@{
ViewBag.Title = "Product Listing"
}
@using PagedList.Mvc; //import this so we get our HTML Helper
@using PagedList; //import this so we can cast our list to IPagedList (only necessary because ViewBag is dynamic)
<!-- import the included stylesheet for some (very basic) default styling -->
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
<!-- loop through each of your products and display it however you want. we're just printing the name here -->
<h2>List of Products</h2>
<ul>
@foreach(var product in ViewBag.OnePageOfProducts){
<li>@product.Name</li>
}
</ul>
<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
@Html.PagedListPager( (IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }) )
Другие работающие ссылки включают в себя:
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/