Search code examples
c#paginationasp.net-core-mvcserver-side

Server-Side Paging MVC 6.0


I have MVC project with WCF service.

When I display a list of data, I do want to load everything from the database/service and do a client paging. But I do want a server-side paging. If I have 100 records and my page size is 10, then when a user clicks on page 1, it will only retrieve the first 10 records from the database and if a user clicks on Page 3, then it will only retrieve the corresponding ten records. I am not using Angular or any other bootstrap.

Can someone guide me how to do it?

  public ActionResult Index(int pageNo = 1)
    {
        ..
        ..
        ..      

        MyViewModel[] myViewModelListArray = MyService.GetData();           

        //when I create this PageList, BLL.GetData have to retreive all the records  to show more than a single page no. 
        //But if the BLL.GetData() was changed to retrieve a subset, then it only shows a single page no.
        //what I wanted to do is, show the correct no of pages (if there are 50 records, and pageSize is 10, then show 
        //page 1,2,3,4,5 and only retrieve 10 records at a time.
        PagedList<MyViewModel> pageList = new PagedList<<MyViewModel>(myViewModelListArray, pageNo, pageSizeListing);
        ..
        ..
        ..
        return View(pageList);
    }

Solution

  • Add paramters page size and page number to your service method and make the result an object which returns TotalCount and a List Items (Items being the items on the current page). Then you can use those values to create the PagedList.

    Inside your business logic code you will do two queries one for the count of items and one for the items on the page.

    Also if you are starting the project now do yourself a favor and remove the useless WCF service from your architecture.