Modify Asp.net MVC pagination control mvcpager; use range increased

Source: Internet
Author: User

In fact, there is nothing, that is, record it and share it with you.

 

As a project of Asp.net MVC 2.0, the paging function must be used. I searched the internet a few times. By chance, I found the mvcpager paging control, and I felt like it very much. (PS: The buddy who writes this control is a person with hearing impairment. I admire it here, depending on the Early Recovery)

 

The usage method is as follows:

 

You can simply query the. topagedlist (pageindex, pagesize) in your own LINQ query set, which is really convenient. Put it in the project immediately. This is a tragedy. The topagedlist (***) method cannot be found. Cause Analysis: topagedlist should be an extension method. I have not used LINQ in my project. querying all lists returned by the Basic ado.net reader <t> set. I feel the problem is definitely here. There are no extension methods such as list <t> set, that is, ienumerable <t>. I searched the mvcpager source code online, haha. You can see it in codeplex. Thanks to cnblogs, csdn, msdn, And If You Are the One. Open Source: I am open source. Haha.

 

The next step is to download the source code, which is very small. Open the project and you will see a class: pagelinqextensions. cs. It must be literally understood. Open it and it looks like this:

 

 

Code

Public   Static   Class Pagelinqextensions
{
Public   Static Pagedlist < T > Topagedlist < T >
(
This Iqueryable < T > Allitems,
Int Pageindex,
Int Pagesize
)
{
If (Pageindex <   1 )
Pageindex
=   1 ;
VaR itemindex
= (Pageindex - 1 ) * Pagesize;
VaR pageofitems
= Allitems. Skip (itemindex). Take (pagesize );
VaR totalitemcount
= Allitems. Count ();
Return   New Pagedlist < T > (Pageofitems, pageindex, pagesize, totalitemcount );
}
}

 

Refer to the type parameter in the topagedlist <t> extension method. iqueryable <t> is correct, so list <t> conversion is not supported. As a result, no matter the class name rules are not standardized, add the followingCode:

 

 

Code

  Public   Static Pagedlist < T > Topagedlist < T > ( This Ienumerable < T > Allitems, Int Pageindex, Int Pagesize)
{
Pageindex
= Pageindex < 1 ? 1 : Pageindex;
VaR itemindex
= (Pageindex - 1 ) * Pagesize;
VaR pageofitems
= Allitems. Skip (itemindex). Take (pagesize );
VaR totalitemcount
= Allitems. Count ();
Return   New Pagedlist < T > (Pageofitems, pageindex, pagesize, totalitemcount );
}

 

 

An extension method is overloaded, and the type parameter is ienumerable <t>. This overload is used to support topagedlist (***) Paging conversion of list <t>. So far. Project test is available.

 

PS: although the current popularity of LINQ and Entity Framework is getting more and more popular, the former ado.net is still widely used. So you have to consider it. (If there is any error, you are welcome to testify. If you find the fault, you will be immediately dismounted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.