A few days ago I saw someone writing ASP. NET MVC on the blog, which made me think of pagedlist. Pagedlist is a paged class library available on NuGet that can be paginated for any ienumerable<t> and very easy to use. From NuGet, you get two DLL:PagedList.dll and PagedList.Mvc.dll. PagedList.dll provides the core operation of paging, and PagedList.Mvc.dll is an auxiliary class library that provides a simple, extensible way to create a paged UI. However, PagedList.dll can be used for MVC2 and above, but pagedlist. Mvc.dll can only be used for MVC3 (and above).
Using pagedlist:
(i), installation pagedlist: Reference-->add Library Package Reference--->online all---> Search pagedlist, click Install Installation. (If you do not have NuGet installed, you can download it at the following address: http://www.nuget.org/)
(b), the advantage of NuGet is that we do not have to do a variety of complex configuration such as Web. config, so the following direct code:
Using Pagedlist;.....//controller:personcontrollerpublic ViewResult Index (int? page) { int pagenumber = page?? 1;< C1/>int pageSize = 2; var persons = db. Persons.tolist (); return View (persons. Topagedlist (PageNumber, pageSize));} ...//view:views/person/index
@model pagedlist.pagedlist<xxx. Person>
... <div>page @ (Model.pagecount < model.pagenumber 0:model.pagenumber) of @Model. pagecount@if ( Model.haspreviouspage) {@Html. ActionLink ("<<", "Index", new {page = 1}) @Html. Raw ("") @Html. ActionLink ("< Prve "," Index ", new {page = model.pagenumber-1})}else{@:<< @Html. Raw (" ") @:< Prev} @Html. Raw (" ") @if (Model.hasnext Page) {@Html. ActionLink ("Next >", "Index", new {page = Model.pagenumber + 1}) @Html. Raw ("") @Html. ActionLink (">> "," Index ", new {page = Model.pagecount})}else{@:next> @Html. Raw (" ") @:>>}</div>
Is it simple? A simple topagelist will return a strongly typed Pagedlist.pagedlist<t> object, and pagedlist.pagedlist<t> implements the Ipagedlist interface. Through the Object Browser we can see that Ipagedlist provides many methods and properties for us to use in view bindings (such as Haspreviouspage, Hasnextpage, PageCount, Pagenumer, etc.), such as:
But there are two more questions:
First, look at the controller in the code, the first feeling is bad. Db. Persons.tolist () Too dangerous, ToList time data has been executed, fantasy, if it is million or even tens data ... So it doesn't work. Because there's no paging effect here. Solve ideas, and quickly organize their implementation.
Second, the view of binding paging there is no better way, each time it is too laborious to write it? The answer is yes, PagedList.Mvc.dll provides the paging navigation function.
Optimize paging with pagedlist:
Controller:personcontrollerpublic ViewResult Index (int page) { int pagenumber = page?? 1; int pageSize = 2; Skip must be preceded by the var persons = from p in db. Persons p.personid Descending select p; return View (persons. Topagedlist (PageNumber, pageSize));}
OK, so you can block the execution of the query before paging. But the view here is still very empty. Pagedlist provides us with the Staticpagedlist<t> class, and I personally prefer to use it for paging queries:
Public Staticpagedlist (system.collections.generic.ienumerable<t> subset, int pagenumber, int pageSize, int Totalitemcount)
As you can see, staticpagedlist needs to pass in a page's data, page numbers, the capacity of each page of data, and the total data entry. That is to say that this time staticpagedlist no longer like Pagedlist to undertake the division of data work, and only assume the data binding operation. Good. Look at an example:
Public ViewResult indextwo (int? page) { int pageIndex = page?? 1; int pageSize = 2; int totalcount = 0; var persons = Getperson (PageIndex, pageSize, ref totalcount); var personsasipagedlist = new staticpagedlist<person> (persons, PageIndex, PageSize, totalcount); Return View (personsasipagedlist);} Public list<person> Getperson (int pageIndex, int. pageSize, ref int totalcount) { var persons = (from P in db). Persons p.personid Descending select p). Skip ((pageIndex-1) * pageSize). Take (pageSize); TotalCount = db. Persons.count (); return persons. ToList ();}
But notice the @model pagedlist.pagedlist<xxx in the view. Person> to be replaced by @model pagedlist.staticpagedlist<xxx. Person>, because the strongly typed objects returned are not the same.
PAGEDLIST.MVC Setting Paging Navigation:
Install Package.Mvc.dll as above, and then see how we can kill page navigation in seconds:
@model pagedlist.staticpagedlist<xxx. person> @using pagedlist@using pagedlist.mvc......<link href= "/content/pagedlist.css" rel= "stylesheet" type= " Text/css "/>......<div> @Html. Pagedlistpager (ipagedlist) Model, page = Url.action (" Indexpagedlistmvc ", new {page = page}) </div>
Replace the code in the previous view:views/person/index with the above code, and you can see that the paging navigation is almost the same as above. PAGEDLISTED.MVC encapsulates the code for pager navigation, such as:
You can see that the above HtmlHelper provides two extension methods mainly: Pagedlistpager and Pagedlistgotopageform. Which Pagedlistpager mainly provide "previous page, next page ..." This type of navigation (not know how to describe here), and Pagedlistgotopageform provides input page to click on a bar-like navigation mode, Pagedlistrenderoptions and Gotoformrenderoptions, respectively, ask them for configuration options.
Pagedlistpager configuration options
The example above uses the default configuration options, Pagedlistrenderoptions also provides additional configuration options, in the following format:
@Html. Pagedlistpager (ipagedlist) Model, page = url.action ("Indexpagedlistmvc", new {page = page}), Pagedli STRENDEROPTIONS.XXX);
Pagedlistrenderoptions.xxx provides more configuration properties, and the configuration options described above are only a combination of these configuration properties, and we can also configure our own property combinations as follows:
@Html. Pagedlistpager (IPage dlist) Model, page = url.action ("Indexpagedlistmvc", new {page = page}), New pagedlistrenderoptions{ Linktopreviouspageformat = "Previous Page", Linktonextpageformat = "Next page", maximumpagenumberstodisplay=5 }); The more simple the
, the easier it looks. Hahaha In addition to this, Pagedlist provides us with a style for pagination navigation. The above <link href= "/content/pagedlist.css" rel= "stylesheet" type= "Text/css"/> are the styles that introduce pager navigation. You install the PAGEDLIST.MVC will automatically put in your content, this is the benefits of NuGet ah .... Of course you can also customize the CSS and reference other CSS, here is recommended a most by the fire of the Twiter BootStrap (official address is: http://twitter.github.com/bootstrap/, said to not understand .... What 12 grid system etc see I am confused) style:
<link rel= "stylesheet" href= "http://twitter.github.com/bootstrap/assets/css/ Bootstrap.css,
Pagedlistgotopagefrom and its configuration
@Html. Pagedlistgotopageform ((ipagedlist) Model, "Indexpagedlistmvc");
@Html. Pagedlistgotopageform ((ipagedlist) Model, "Indexpagedlistmvc", new Gotoformrenderoptions {xxx=xxx});
Unlike Pagedlistrenderoptions, the gotofromrenderoptions has no configuration options, and the others are similar.
So far, seemingly two problems have been completely solved, but the good user experience of the page certainly do not want to click on the next page after the page refresh operation-------Ajax. Because it involves jquery.templ.js, and I have not seen this stuff before, so first put down, and then to say the pagedlist+ajax of the pagination and pagedlist implementation principle.
ASP. NET MVC leverages pagedlist pagination (i)