1

I want to implement Server side pagination for loading of some data I want to be loaded into browser. It's working fine Client side with PageList in MVC but I don't know how to do in Asp.net Core Server side.

This is my Class There I want to show all proporties , even photo (image)

public class HouseDTO
    {
        [Key]
        public int HouseId { get; set; }
        public Nullable<decimal> Price { get; set; }
        public string LiveArea { get; set; }
        public string RoomAmount { get; set; }
        public string HouseType { get; set; }
        public string ImageName { get; set; }
    } 

And then my Repisitory

public interface IHouseRepository
{

  public IEnumerable<HouseDTO> GetAllHouses()

}

 public class HouseRepository : IHouseRepository
 {

    private ApplicationDbContext db;

    public HouseRepository(ApplicationDbContext db)
    {
            this.db = db;
    }

    public IEnumerable<HouseDTO> GetAllHouses()
    {
            return db.Houses;
    }
}

And this is my Controller

public class AdvController : Controller
{

   private IHouseRepository db;
   private IHostingEnvironment hostingEnvirnment;

   public AdvController(IHouseRepository db, IHostingEnvironment hostingEnvirnment)
   {
      this.db = db;
      this.hostingEnvirnment = hostingEnvirnment;

   }

   public IActionResult Index()
   {
     var model = db.GetAllHouses();  // How can I do this to Server side pagination?
     return View(model);
   } 
}

So How can create Server side Pagination for this action?

public IActionResult Index()
{
   var model = db.GetAllHouses();   
   return View(model);
}

I would greatly appreciate it if you help me.

Anna
  • 69
  • 1
  • 10
  • Note that all you need to do/know is already covered in question you've likely seen in your research - https://stackoverflow.com/questions/446196/how-do-i-do-pagination-in-asp-net-mvc. Some clarification why that was not enough could help targeting answer. – Alexei Levenkov Oct 21 '19 at 21:00

3 Answers3

3

You can use Skip() and Take(). Make a repository method that will take current position (to skip) and give parameter to Take. Something like:

public House GetPaged(currentPosition)
{
  return db.Houses.Skip(currentPosition).Take(20);
}
  • 1
    This shouldn't call `db.GetAllHouses()`, though, because that's reducing it from `IQueryable` to `IEnumerable`, which will end up pulling all records before applying `Skip` and `Take. Change that to `db.Houses` (and expose the method instead of making it private) and you're good. The repository *is* the better place for it. – madreflection Oct 21 '19 at 20:48
  • I am sure that a service class, using repository is the best place for storing such business logic. And then controller should use service method, not repository directly. – Ljubomir Bacovic Oct 21 '19 at 20:50
  • 1
    That's not the distinction I was trying to make. Compare this to Dmytro's answer, which suggests doing this in the controller. That's not good because it assumes that `GetAllHouses` doesn't return a reified set. Your answer is better (the crux of my point). – madreflection Oct 21 '19 at 20:51
  • I see. Thanks. I guess I was trying to improve it further - do what I would do in my code. – Ljubomir Bacovic Oct 21 '19 at 20:53
  • Edited my answer to include suggestions from @madreflection – Ljubomir Bacovic Oct 21 '19 at 20:55
  • @Ljubomir Bacovic thank you for your response. I'am new for this kind of staffs, please can you show me more detail how to do? – Anna Oct 21 '19 at 20:57
  • @Anna - as you've likely already found in you research https://stackoverflow.com/questions/446196/how-do-i-do-pagination-in-asp-net-mvc has all the same information with a lot of detail. This answer just rehashes those... – Alexei Levenkov Oct 21 '19 at 21:02
  • @Anna, I assume House is a persistance class? In that case, call it just House, not HouseDTO. In your repository, you can add a new method public IEnumerable GetPagedHouses(int pageNumber, int recordsPerPage) { return db.Houses.Skip(pageNumber*recordsPerPage).Take(recordsPerPage); } In your controller you can call HouseRepository.GetPagedHouses(2, 20); – Ljubomir Bacovic Oct 21 '19 at 21:03
  • @LjubomirBacovic Thank you, Is this Serverside not Client side? I want this to be Server side. – Anna Oct 22 '19 at 18:42
1

Take() and Skip() over results of db.Houses is the way to go.

Like this:

// Skip (n) pages and take (x) elements from required page.
return db.Houses.Skip(page*countPerPage).Take(countPerPage);
// As suggested in comments for the answer above, specified code should belong to 
// repository method. Initially implemented as a template to be copypasted 
// and reused according to your needs.

make sure that page numbering in query is 0-based: page = 0 if page not specified; page = 0 if you require page #1; page = 1 if you need page #2 etc. And countPerPage meaning is obvious :)

Dmytro
  • 1,590
  • 14
  • 14
  • thank you for your response. I'am new for this kind of staffs, please can you show me more detail how to do? – Anna Oct 21 '19 at 20:57
0

I might be a bit late for the party but I wrote a lightweight package to address this issue by giving you the toolkit to build your DB queries using Skip() and Take() as the other answers suggested.

This might be helpful for someone googling around: https://github.com/YannikG/dotnet-pageable-data

Yannik
  • 887
  • 3
  • 9
  • 29