user2463553
user2463553

Reputation: 65

PagedList Pagination

I'm using PagedList in MVC . Everything works fine but the scenario goes like this.

Loading data from Stored Procedure.

So when an search condition satisfy SP returns 200 Records , Pagination goes like Page 1 2 3 4 .. ( Actual wanted Scenario) which is working as expected and PageSize 20.

But when requirement changes

When Page 1 Click get 20 records , Page 2 get 20 Records and so on ( each click from Database ).. So on initial load SP will return only 20 Records rather than actual (200 records ) only one Pagination Page 1 Appears

But we know actual 200 records pagination should be Page 1 Page 2 Page 3 it fails . So Pagination is created for only 20 records rather than actual number ie 200 .

Yes the work around is SP returns as output total 200 records so i know how many total records but how to overwrite Html.PagedListPager to show Page 1 2 3 ?????

Thanks in advance

Upvotes: 1

Views: 2594

Answers (2)

Vinod
Vinod

Reputation: 566

Yep StaticPagedList serves the purposes .

Few changes Required are :

var usersAsIPagedList = new StaticPagedList<CLASSNAME>(LIST, PAGEINDEX, PAGESIZE, TOTALCOUNT);

here TOTALCOUNT is 200 for your example.

While returning view make sure to return usersAsIPagedList and not as usersAsIPagedList.ToPagedList

return PartialView("_PARTIALVIEW", usersAsIPagedList);

Upvotes: 3

Ala&#39; Alnajjar
Ala&#39; Alnajjar

Reputation: 798

You have to use manual paging StaticPagedList,you have to modify SP to return the total number of records along with the results

This is an example quoted from "PagedList project" website :

public class UserController : Controller
{
    public object Index(int? page)
    {
        var pageIndex = (page ?? 1) - 1; //MembershipProvider expects a 0 for the first page
        var pageSize = 10;
        int totalUserCount; // will be set by call to GetAllUsers due to _out_ paramter :-|

        var users = Membership.GetAllUsers(pageIndex, pageSize, out totalUserCount);
        var usersAsIPagedList = new StaticPagedList<MembershipUser>(users, pageIndex + 1, pageSize, totalUserCount);

        ViewBag.OnePageOfUsers = usersAsIPagedList;
        return View();
    }
}

Upvotes: 1

Related Questions