PagedList Pagination

本秂侑毒 提交于 2019-12-11 11:45:02

问题


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


回答1:


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);



回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/27600517/pagedlist-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!