简体   繁体   中英

How to write code for Server side pagination in ASP.NET Core

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM