Search code examples
asp.net-mvc-3paginationpagedlist

Paging with PagedList


This is my repository layer:

public List<Section> GetAllSections()
{
    return context.Sections.Include("Groups").Include("Groups.Classes").ToList();
}

This is my application layer:

public List<Section> GetAllSections()
{
    return cacheSectionRepo.GetAllSections();
}

And in controller I have:

SectionApplication sectionApp = new SectionApplication();
public ActionResult Index()
{
    return View(sectionApp.GetAllSections());
}

Now I want to make my Index view paged.I want to use from PagedList.How should I do this? For example I want every page shows 5 records.


Solution

  • You could pass a page number and page size to the repository layer, and then use the Skip and Take Linq statements to filter out the rows you need:

    public List<Section> GetAllSections(int pageSize=5, int pageNo)
    {
        return cacheSectionRepo.GetAllSections().Skip(pageSize * pageNo).Take(pageSize);;
    }
    

    alternatively you could perform that filtering in you r repository layer. Then with every request to the controller, you would send the pageNo to the controller, preferably through an AJAX request.