How to implement pagination in asp.net core razor pages











up vote
1
down vote

favorite
1












I'm learning asp.net core razor pages with ef. I want to implement pagination with my table, I have check this tutorial



https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.1



but it only support pre and next, I have researched for a long time, all of the solution are related to asp.net core mvc, but I'm using razor pages, there's no controller in my project, any ideas to implement?



This is the effect what I want to implement



enter image description here



 <form method="get" asp-page="./Index">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
@{
var totalPages = Model.Products.Count % 2 == 0 ? Model.Products.Count / 2 : Model.Products.Count / 2 + 1;
}
@for (int i = 1; i <= totalPages; i++)
{
<li><a asp-page="./Index" asp-route-id="@i">@i</a></li>
}
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</form>


cshtml.cs



  public async Task OnGetAsync(string sortOrder, string searchString, string shopString, string statusString, int page)
{}









share|improve this question
























  • @Epistaxis any documentation about it? Thanks
    – Allen4Tech
    Jun 19 at 12:53










  • @Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
    – Allen4Tech
    Jun 19 at 13:01















up vote
1
down vote

favorite
1












I'm learning asp.net core razor pages with ef. I want to implement pagination with my table, I have check this tutorial



https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.1



but it only support pre and next, I have researched for a long time, all of the solution are related to asp.net core mvc, but I'm using razor pages, there's no controller in my project, any ideas to implement?



This is the effect what I want to implement



enter image description here



 <form method="get" asp-page="./Index">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
@{
var totalPages = Model.Products.Count % 2 == 0 ? Model.Products.Count / 2 : Model.Products.Count / 2 + 1;
}
@for (int i = 1; i <= totalPages; i++)
{
<li><a asp-page="./Index" asp-route-id="@i">@i</a></li>
}
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</form>


cshtml.cs



  public async Task OnGetAsync(string sortOrder, string searchString, string shopString, string statusString, int page)
{}









share|improve this question
























  • @Epistaxis any documentation about it? Thanks
    – Allen4Tech
    Jun 19 at 12:53










  • @Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
    – Allen4Tech
    Jun 19 at 13:01













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I'm learning asp.net core razor pages with ef. I want to implement pagination with my table, I have check this tutorial



https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.1



but it only support pre and next, I have researched for a long time, all of the solution are related to asp.net core mvc, but I'm using razor pages, there's no controller in my project, any ideas to implement?



This is the effect what I want to implement



enter image description here



 <form method="get" asp-page="./Index">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
@{
var totalPages = Model.Products.Count % 2 == 0 ? Model.Products.Count / 2 : Model.Products.Count / 2 + 1;
}
@for (int i = 1; i <= totalPages; i++)
{
<li><a asp-page="./Index" asp-route-id="@i">@i</a></li>
}
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</form>


cshtml.cs



  public async Task OnGetAsync(string sortOrder, string searchString, string shopString, string statusString, int page)
{}









share|improve this question















I'm learning asp.net core razor pages with ef. I want to implement pagination with my table, I have check this tutorial



https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.1



but it only support pre and next, I have researched for a long time, all of the solution are related to asp.net core mvc, but I'm using razor pages, there's no controller in my project, any ideas to implement?



This is the effect what I want to implement



enter image description here



 <form method="get" asp-page="./Index">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
@{
var totalPages = Model.Products.Count % 2 == 0 ? Model.Products.Count / 2 : Model.Products.Count / 2 + 1;
}
@for (int i = 1; i <= totalPages; i++)
{
<li><a asp-page="./Index" asp-route-id="@i">@i</a></li>
}
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</form>


cshtml.cs



  public async Task OnGetAsync(string sortOrder, string searchString, string shopString, string statusString, int page)
{}






c# razor asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 19 at 13:41

























asked Jun 19 at 12:49









Allen4Tech

1,19011144




1,19011144












  • @Epistaxis any documentation about it? Thanks
    – Allen4Tech
    Jun 19 at 12:53










  • @Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
    – Allen4Tech
    Jun 19 at 13:01


















  • @Epistaxis any documentation about it? Thanks
    – Allen4Tech
    Jun 19 at 12:53










  • @Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
    – Allen4Tech
    Jun 19 at 13:01
















@Epistaxis any documentation about it? Thanks
– Allen4Tech
Jun 19 at 12:53




@Epistaxis any documentation about it? Thanks
– Allen4Tech
Jun 19 at 12:53












@Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
– Allen4Tech
Jun 19 at 13:01




@Epistaxis I checked the code, but it looks like it need a controller, I have found lots of package to support it, but I have no controller, I use razor pages only. Thanks
– Allen4Tech
Jun 19 at 13:01












4 Answers
4






active

oldest

votes

















up vote
7
down vote



accepted










Pagination is relatively simple. There's libraries available to do it for you, but I've started to find them more trouble than they're worth.



You need three pieces of information from the request (or set to default values):




  1. Page number (default to 1)

  2. Page size (typically defaults to 10, but whatever you want)

  3. Sort (not strictly necessary, but you should at least order by something to keep the results consistent across pages)


The page number and size give you your "skip" and "take" values:



var skip = (page - 1) * size;
var take = size;


You can then fetch the results via:



var pageOfResults = await query.Skip(skip).Take(take).ToListAsync();


Where query is an IQueryable - either your DbSet directly or the DbSet with a Where clause, OrderBy, etc. applied.



Then, you just need to the total number of items to figure the pages:



var count = await query.CountAsync();


Pro Tip, you can parallelize the two queries (results and total count) by doing:



var resultsTask = query.Skip(skip).Take(take).ToListAsync();
var countTask = query.CountAsync();

var results = await resultsTask;
var count = await countTask;


Tasks return hot, or already started. The await keyword simply holds the continuation of the rest of the code until the task completes. As a result, if you await each line, they'll complete in serial, but if you start both, first, and then await each, they'll process in parallel.



Anyways, once you have the count:



var totalPages = (int)Math.Ceil(Decimal.Divide(count, size));
var firstPage = 1;
var lastPage = totalPages;
var prevPage = Math.Max(page - 1, firstPage);
var nextPage = Math.Min(page + 1, lastPage);


Note: you can determine whether to show first/previous and last/next buttons based on whether they equal firstPage or lastPage, respectively.



Then, just build yourself a model with this information, and you can send that to the view to render the results and generate the paging HTML.






share|improve this answer





















  • I appreciate how well this answer is explained. Good answer.
    – Nkosi
    Jun 19 at 13:40










  • If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
    – Eddie Paz
    Sep 25 at 18:37










  • Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
    – Chris Pratt
    Sep 25 at 19:13










  • Awesome thank you so much.
    – Muhmmad Abubakar Ikram
    Nov 23 at 11:00


















up vote
0
down vote













You can use the JW.Pager NuGet package (https://www.nuget.org/packages/JW.Pager/)



Here's an example razor pages page model that paginates a list of 150 items:



using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.RazorPages;
using JW;

namespace RazorPagesPagination.Pages
{
public class IndexModel : PageModel
{
public IEnumerable<string> Items { get; set; }
public Pager Pager { get; set; }

public void OnGet(int p = 1)
{
// generate list of sample items to be paged
var dummyItems = Enumerable.Range(1, 150).Select(x => "Item " + x);

// get pagination info for the current page
Pager = new Pager(dummyItems.Count(), p);

// assign the current page of items to the Items property
Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
}
}
}


And here's the razor pages page containing the html for the paged list and pager controls:



@page
@model RazorPagesPagination.Pages.IndexModel

<!-- items being paged -->
<table class="table table-sm table-striped table-bordered">
@foreach (var item in Model.Items)
{
<tr>
<td>@item</td>
</tr>
}
</table>

<!-- pager -->
@if (Model.Pager.Pages.Any())
{
<nav class="table-responsive">
<ul class="pagination justify-content-center d-flex flex-wrap">
@if (Model.Pager.CurrentPage > 1)
{
<li class="page-item">
<a class="page-link" href="/">First</a>
</li>
<li class="page-item">
<a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
</li>
}

@foreach (var p in Model.Pager.Pages)
{
<li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
<a class="page-link" href="/?p=@p">@p</a>
</li>
}

@if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
{
<li class="page-item">
<a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
</li>
<li class="page-item">
<a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
</li>
}
</ul>
</nav>
}


For more details I posted a full tutorial with example project at http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example






share|improve this answer




























    up vote
    0
    down vote













    I have created a paging taghelper for .net Core Razor Pages, it can be configured within html tags or appsettings.json to show/ hide prev-next, first-last buttons, number of max displayed pages and more customization settings are available,



    The project is available to install as nuget package (and github):



    Install-Package LazZiya.TagHelpers -Version 1.0.2


    See installation steps here:
    http://ziyad.info/en/articles/21-Paging_TagHelper_for_ASP_NET_Core



    Live demo and all settings:
    http://demo.ziyad.info/en/paging



    Using appsettings.json for paging configurations will help to have more clean html code and it gives the ability to change paging settings on all or specific paging taghelpers in your application.






    share|improve this answer




























      up vote
      0
      down vote













      I made this implementation mixing together a few answers on the subject, I hope it helps someone.



      Add a PagedResultBase class (that you can extend adding other properties you need):



      public abstract class PagedResultBase
      {
      public int CurrentPage { get; set; }
      public int PageCount { get; set; }
      public int PageSize { get; set; }
      public int RowCount { get; set; }
      }


      Add a PagedResult class:



      public class PagedResult<T> : PagedResultBase where T : class
      {
      public ICollection<T> Results { get; set; }

      public PagedResult()
      {
      Results = new List<T>();
      }
      }


      Add a IQueryableExtensions with a GetPagedResult extension:



      public static class IQueryableExtensions
      {
      public async static Task<PagedResult<T>> GetPagedResultAsync<T>(this IQueryable<T> query, int currentPage, int pageSize) where T : class
      {
      var skip = (currentPage - 1) * pageSize;
      var take = pageSize;

      var rowCount = await query.CountAsync();
      var results = await query.Skip(skip).Take(take).ToListAsync();

      var pagedResult = new PagedResult<T> {
      CurrentPage = currentPage,
      PageCount = (int)Math.Ceiling(decimal.Divide(rowCount, pageSize)),
      PageSize = pageSize,
      RowCount = rowCount,
      Results = results
      };

      return pagedResult;
      }
      }


      You are done:



      var pagedResult = await MyContext.Posts.Where(p => p.Featured == true).GetPagedResultAsync(1, 10);





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50928891%2fhow-to-implement-pagination-in-asp-net-core-razor-pages%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        7
        down vote



        accepted










        Pagination is relatively simple. There's libraries available to do it for you, but I've started to find them more trouble than they're worth.



        You need three pieces of information from the request (or set to default values):




        1. Page number (default to 1)

        2. Page size (typically defaults to 10, but whatever you want)

        3. Sort (not strictly necessary, but you should at least order by something to keep the results consistent across pages)


        The page number and size give you your "skip" and "take" values:



        var skip = (page - 1) * size;
        var take = size;


        You can then fetch the results via:



        var pageOfResults = await query.Skip(skip).Take(take).ToListAsync();


        Where query is an IQueryable - either your DbSet directly or the DbSet with a Where clause, OrderBy, etc. applied.



        Then, you just need to the total number of items to figure the pages:



        var count = await query.CountAsync();


        Pro Tip, you can parallelize the two queries (results and total count) by doing:



        var resultsTask = query.Skip(skip).Take(take).ToListAsync();
        var countTask = query.CountAsync();

        var results = await resultsTask;
        var count = await countTask;


        Tasks return hot, or already started. The await keyword simply holds the continuation of the rest of the code until the task completes. As a result, if you await each line, they'll complete in serial, but if you start both, first, and then await each, they'll process in parallel.



        Anyways, once you have the count:



        var totalPages = (int)Math.Ceil(Decimal.Divide(count, size));
        var firstPage = 1;
        var lastPage = totalPages;
        var prevPage = Math.Max(page - 1, firstPage);
        var nextPage = Math.Min(page + 1, lastPage);


        Note: you can determine whether to show first/previous and last/next buttons based on whether they equal firstPage or lastPage, respectively.



        Then, just build yourself a model with this information, and you can send that to the view to render the results and generate the paging HTML.






        share|improve this answer





















        • I appreciate how well this answer is explained. Good answer.
          – Nkosi
          Jun 19 at 13:40










        • If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
          – Eddie Paz
          Sep 25 at 18:37










        • Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
          – Chris Pratt
          Sep 25 at 19:13










        • Awesome thank you so much.
          – Muhmmad Abubakar Ikram
          Nov 23 at 11:00















        up vote
        7
        down vote



        accepted










        Pagination is relatively simple. There's libraries available to do it for you, but I've started to find them more trouble than they're worth.



        You need three pieces of information from the request (or set to default values):




        1. Page number (default to 1)

        2. Page size (typically defaults to 10, but whatever you want)

        3. Sort (not strictly necessary, but you should at least order by something to keep the results consistent across pages)


        The page number and size give you your "skip" and "take" values:



        var skip = (page - 1) * size;
        var take = size;


        You can then fetch the results via:



        var pageOfResults = await query.Skip(skip).Take(take).ToListAsync();


        Where query is an IQueryable - either your DbSet directly or the DbSet with a Where clause, OrderBy, etc. applied.



        Then, you just need to the total number of items to figure the pages:



        var count = await query.CountAsync();


        Pro Tip, you can parallelize the two queries (results and total count) by doing:



        var resultsTask = query.Skip(skip).Take(take).ToListAsync();
        var countTask = query.CountAsync();

        var results = await resultsTask;
        var count = await countTask;


        Tasks return hot, or already started. The await keyword simply holds the continuation of the rest of the code until the task completes. As a result, if you await each line, they'll complete in serial, but if you start both, first, and then await each, they'll process in parallel.



        Anyways, once you have the count:



        var totalPages = (int)Math.Ceil(Decimal.Divide(count, size));
        var firstPage = 1;
        var lastPage = totalPages;
        var prevPage = Math.Max(page - 1, firstPage);
        var nextPage = Math.Min(page + 1, lastPage);


        Note: you can determine whether to show first/previous and last/next buttons based on whether they equal firstPage or lastPage, respectively.



        Then, just build yourself a model with this information, and you can send that to the view to render the results and generate the paging HTML.






        share|improve this answer





















        • I appreciate how well this answer is explained. Good answer.
          – Nkosi
          Jun 19 at 13:40










        • If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
          – Eddie Paz
          Sep 25 at 18:37










        • Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
          – Chris Pratt
          Sep 25 at 19:13










        • Awesome thank you so much.
          – Muhmmad Abubakar Ikram
          Nov 23 at 11:00













        up vote
        7
        down vote



        accepted







        up vote
        7
        down vote



        accepted






        Pagination is relatively simple. There's libraries available to do it for you, but I've started to find them more trouble than they're worth.



        You need three pieces of information from the request (or set to default values):




        1. Page number (default to 1)

        2. Page size (typically defaults to 10, but whatever you want)

        3. Sort (not strictly necessary, but you should at least order by something to keep the results consistent across pages)


        The page number and size give you your "skip" and "take" values:



        var skip = (page - 1) * size;
        var take = size;


        You can then fetch the results via:



        var pageOfResults = await query.Skip(skip).Take(take).ToListAsync();


        Where query is an IQueryable - either your DbSet directly or the DbSet with a Where clause, OrderBy, etc. applied.



        Then, you just need to the total number of items to figure the pages:



        var count = await query.CountAsync();


        Pro Tip, you can parallelize the two queries (results and total count) by doing:



        var resultsTask = query.Skip(skip).Take(take).ToListAsync();
        var countTask = query.CountAsync();

        var results = await resultsTask;
        var count = await countTask;


        Tasks return hot, or already started. The await keyword simply holds the continuation of the rest of the code until the task completes. As a result, if you await each line, they'll complete in serial, but if you start both, first, and then await each, they'll process in parallel.



        Anyways, once you have the count:



        var totalPages = (int)Math.Ceil(Decimal.Divide(count, size));
        var firstPage = 1;
        var lastPage = totalPages;
        var prevPage = Math.Max(page - 1, firstPage);
        var nextPage = Math.Min(page + 1, lastPage);


        Note: you can determine whether to show first/previous and last/next buttons based on whether they equal firstPage or lastPage, respectively.



        Then, just build yourself a model with this information, and you can send that to the view to render the results and generate the paging HTML.






        share|improve this answer












        Pagination is relatively simple. There's libraries available to do it for you, but I've started to find them more trouble than they're worth.



        You need three pieces of information from the request (or set to default values):




        1. Page number (default to 1)

        2. Page size (typically defaults to 10, but whatever you want)

        3. Sort (not strictly necessary, but you should at least order by something to keep the results consistent across pages)


        The page number and size give you your "skip" and "take" values:



        var skip = (page - 1) * size;
        var take = size;


        You can then fetch the results via:



        var pageOfResults = await query.Skip(skip).Take(take).ToListAsync();


        Where query is an IQueryable - either your DbSet directly or the DbSet with a Where clause, OrderBy, etc. applied.



        Then, you just need to the total number of items to figure the pages:



        var count = await query.CountAsync();


        Pro Tip, you can parallelize the two queries (results and total count) by doing:



        var resultsTask = query.Skip(skip).Take(take).ToListAsync();
        var countTask = query.CountAsync();

        var results = await resultsTask;
        var count = await countTask;


        Tasks return hot, or already started. The await keyword simply holds the continuation of the rest of the code until the task completes. As a result, if you await each line, they'll complete in serial, but if you start both, first, and then await each, they'll process in parallel.



        Anyways, once you have the count:



        var totalPages = (int)Math.Ceil(Decimal.Divide(count, size));
        var firstPage = 1;
        var lastPage = totalPages;
        var prevPage = Math.Max(page - 1, firstPage);
        var nextPage = Math.Min(page + 1, lastPage);


        Note: you can determine whether to show first/previous and last/next buttons based on whether they equal firstPage or lastPage, respectively.



        Then, just build yourself a model with this information, and you can send that to the view to render the results and generate the paging HTML.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 19 at 13:15









        Chris Pratt

        150k20230295




        150k20230295












        • I appreciate how well this answer is explained. Good answer.
          – Nkosi
          Jun 19 at 13:40










        • If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
          – Eddie Paz
          Sep 25 at 18:37










        • Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
          – Chris Pratt
          Sep 25 at 19:13










        • Awesome thank you so much.
          – Muhmmad Abubakar Ikram
          Nov 23 at 11:00


















        • I appreciate how well this answer is explained. Good answer.
          – Nkosi
          Jun 19 at 13:40










        • If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
          – Eddie Paz
          Sep 25 at 18:37










        • Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
          – Chris Pratt
          Sep 25 at 19:13










        • Awesome thank you so much.
          – Muhmmad Abubakar Ikram
          Nov 23 at 11:00
















        I appreciate how well this answer is explained. Good answer.
        – Nkosi
        Jun 19 at 13:40




        I appreciate how well this answer is explained. Good answer.
        – Nkosi
        Jun 19 at 13:40












        If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
        – Eddie Paz
        Sep 25 at 18:37




        If you're using EntityFramework, you cannot use the Pro Tip as EF doesn't support multiple requests on the same context. Bumer! stackoverflow.com/questions/41749896/…
        – Eddie Paz
        Sep 25 at 18:37












        Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
        – Chris Pratt
        Sep 25 at 19:13




        Actually, as far as I'm aware, they simply discourage it. There's not true support for parallelized queries, so certain operations stacked the wrong way can cause issues. However, here, there's just a select and a count be parallelized, so nothing can really go wrong, and last I checked, this code will run as is.
        – Chris Pratt
        Sep 25 at 19:13












        Awesome thank you so much.
        – Muhmmad Abubakar Ikram
        Nov 23 at 11:00




        Awesome thank you so much.
        – Muhmmad Abubakar Ikram
        Nov 23 at 11:00












        up vote
        0
        down vote













        You can use the JW.Pager NuGet package (https://www.nuget.org/packages/JW.Pager/)



        Here's an example razor pages page model that paginates a list of 150 items:



        using System.Collections.Generic;
        using System.Linq;
        using Microsoft.AspNetCore.Mvc.RazorPages;
        using JW;

        namespace RazorPagesPagination.Pages
        {
        public class IndexModel : PageModel
        {
        public IEnumerable<string> Items { get; set; }
        public Pager Pager { get; set; }

        public void OnGet(int p = 1)
        {
        // generate list of sample items to be paged
        var dummyItems = Enumerable.Range(1, 150).Select(x => "Item " + x);

        // get pagination info for the current page
        Pager = new Pager(dummyItems.Count(), p);

        // assign the current page of items to the Items property
        Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
        }
        }
        }


        And here's the razor pages page containing the html for the paged list and pager controls:



        @page
        @model RazorPagesPagination.Pages.IndexModel

        <!-- items being paged -->
        <table class="table table-sm table-striped table-bordered">
        @foreach (var item in Model.Items)
        {
        <tr>
        <td>@item</td>
        </tr>
        }
        </table>

        <!-- pager -->
        @if (Model.Pager.Pages.Any())
        {
        <nav class="table-responsive">
        <ul class="pagination justify-content-center d-flex flex-wrap">
        @if (Model.Pager.CurrentPage > 1)
        {
        <li class="page-item">
        <a class="page-link" href="/">First</a>
        </li>
        <li class="page-item">
        <a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
        </li>
        }

        @foreach (var p in Model.Pager.Pages)
        {
        <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
        <a class="page-link" href="/?p=@p">@p</a>
        </li>
        }

        @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
        {
        <li class="page-item">
        <a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
        </li>
        <li class="page-item">
        <a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
        </li>
        }
        </ul>
        </nav>
        }


        For more details I posted a full tutorial with example project at http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example






        share|improve this answer

























          up vote
          0
          down vote













          You can use the JW.Pager NuGet package (https://www.nuget.org/packages/JW.Pager/)



          Here's an example razor pages page model that paginates a list of 150 items:



          using System.Collections.Generic;
          using System.Linq;
          using Microsoft.AspNetCore.Mvc.RazorPages;
          using JW;

          namespace RazorPagesPagination.Pages
          {
          public class IndexModel : PageModel
          {
          public IEnumerable<string> Items { get; set; }
          public Pager Pager { get; set; }

          public void OnGet(int p = 1)
          {
          // generate list of sample items to be paged
          var dummyItems = Enumerable.Range(1, 150).Select(x => "Item " + x);

          // get pagination info for the current page
          Pager = new Pager(dummyItems.Count(), p);

          // assign the current page of items to the Items property
          Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
          }
          }
          }


          And here's the razor pages page containing the html for the paged list and pager controls:



          @page
          @model RazorPagesPagination.Pages.IndexModel

          <!-- items being paged -->
          <table class="table table-sm table-striped table-bordered">
          @foreach (var item in Model.Items)
          {
          <tr>
          <td>@item</td>
          </tr>
          }
          </table>

          <!-- pager -->
          @if (Model.Pager.Pages.Any())
          {
          <nav class="table-responsive">
          <ul class="pagination justify-content-center d-flex flex-wrap">
          @if (Model.Pager.CurrentPage > 1)
          {
          <li class="page-item">
          <a class="page-link" href="/">First</a>
          </li>
          <li class="page-item">
          <a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
          </li>
          }

          @foreach (var p in Model.Pager.Pages)
          {
          <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
          <a class="page-link" href="/?p=@p">@p</a>
          </li>
          }

          @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
          {
          <li class="page-item">
          <a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
          </li>
          <li class="page-item">
          <a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
          </li>
          }
          </ul>
          </nav>
          }


          For more details I posted a full tutorial with example project at http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example






          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use the JW.Pager NuGet package (https://www.nuget.org/packages/JW.Pager/)



            Here's an example razor pages page model that paginates a list of 150 items:



            using System.Collections.Generic;
            using System.Linq;
            using Microsoft.AspNetCore.Mvc.RazorPages;
            using JW;

            namespace RazorPagesPagination.Pages
            {
            public class IndexModel : PageModel
            {
            public IEnumerable<string> Items { get; set; }
            public Pager Pager { get; set; }

            public void OnGet(int p = 1)
            {
            // generate list of sample items to be paged
            var dummyItems = Enumerable.Range(1, 150).Select(x => "Item " + x);

            // get pagination info for the current page
            Pager = new Pager(dummyItems.Count(), p);

            // assign the current page of items to the Items property
            Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
            }
            }
            }


            And here's the razor pages page containing the html for the paged list and pager controls:



            @page
            @model RazorPagesPagination.Pages.IndexModel

            <!-- items being paged -->
            <table class="table table-sm table-striped table-bordered">
            @foreach (var item in Model.Items)
            {
            <tr>
            <td>@item</td>
            </tr>
            }
            </table>

            <!-- pager -->
            @if (Model.Pager.Pages.Any())
            {
            <nav class="table-responsive">
            <ul class="pagination justify-content-center d-flex flex-wrap">
            @if (Model.Pager.CurrentPage > 1)
            {
            <li class="page-item">
            <a class="page-link" href="/">First</a>
            </li>
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
            </li>
            }

            @foreach (var p in Model.Pager.Pages)
            {
            <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
            <a class="page-link" href="/?p=@p">@p</a>
            </li>
            }

            @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
            {
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
            </li>
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
            </li>
            }
            </ul>
            </nav>
            }


            For more details I posted a full tutorial with example project at http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example






            share|improve this answer












            You can use the JW.Pager NuGet package (https://www.nuget.org/packages/JW.Pager/)



            Here's an example razor pages page model that paginates a list of 150 items:



            using System.Collections.Generic;
            using System.Linq;
            using Microsoft.AspNetCore.Mvc.RazorPages;
            using JW;

            namespace RazorPagesPagination.Pages
            {
            public class IndexModel : PageModel
            {
            public IEnumerable<string> Items { get; set; }
            public Pager Pager { get; set; }

            public void OnGet(int p = 1)
            {
            // generate list of sample items to be paged
            var dummyItems = Enumerable.Range(1, 150).Select(x => "Item " + x);

            // get pagination info for the current page
            Pager = new Pager(dummyItems.Count(), p);

            // assign the current page of items to the Items property
            Items = dummyItems.Skip((Pager.CurrentPage - 1) * Pager.PageSize).Take(Pager.PageSize);
            }
            }
            }


            And here's the razor pages page containing the html for the paged list and pager controls:



            @page
            @model RazorPagesPagination.Pages.IndexModel

            <!-- items being paged -->
            <table class="table table-sm table-striped table-bordered">
            @foreach (var item in Model.Items)
            {
            <tr>
            <td>@item</td>
            </tr>
            }
            </table>

            <!-- pager -->
            @if (Model.Pager.Pages.Any())
            {
            <nav class="table-responsive">
            <ul class="pagination justify-content-center d-flex flex-wrap">
            @if (Model.Pager.CurrentPage > 1)
            {
            <li class="page-item">
            <a class="page-link" href="/">First</a>
            </li>
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage - 1)">Previous</a>
            </li>
            }

            @foreach (var p in Model.Pager.Pages)
            {
            <li class="page-item @(p == Model.Pager.CurrentPage ? "active" : "")">
            <a class="page-link" href="/?p=@p">@p</a>
            </li>
            }

            @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
            {
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.CurrentPage + 1)">Next</a>
            </li>
            <li class="page-item">
            <a class="page-link" href="/?p=@(Model.Pager.TotalPages)">Last</a>
            </li>
            }
            </ul>
            </nav>
            }


            For more details I posted a full tutorial with example project at http://jasonwatmore.com/post/2018/10/15/aspnet-core-razor-pages-pagination-example







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 15 at 14:15









            Jason

            2,9832227




            2,9832227






















                up vote
                0
                down vote













                I have created a paging taghelper for .net Core Razor Pages, it can be configured within html tags or appsettings.json to show/ hide prev-next, first-last buttons, number of max displayed pages and more customization settings are available,



                The project is available to install as nuget package (and github):



                Install-Package LazZiya.TagHelpers -Version 1.0.2


                See installation steps here:
                http://ziyad.info/en/articles/21-Paging_TagHelper_for_ASP_NET_Core



                Live demo and all settings:
                http://demo.ziyad.info/en/paging



                Using appsettings.json for paging configurations will help to have more clean html code and it gives the ability to change paging settings on all or specific paging taghelpers in your application.






                share|improve this answer

























                  up vote
                  0
                  down vote













                  I have created a paging taghelper for .net Core Razor Pages, it can be configured within html tags or appsettings.json to show/ hide prev-next, first-last buttons, number of max displayed pages and more customization settings are available,



                  The project is available to install as nuget package (and github):



                  Install-Package LazZiya.TagHelpers -Version 1.0.2


                  See installation steps here:
                  http://ziyad.info/en/articles/21-Paging_TagHelper_for_ASP_NET_Core



                  Live demo and all settings:
                  http://demo.ziyad.info/en/paging



                  Using appsettings.json for paging configurations will help to have more clean html code and it gives the ability to change paging settings on all or specific paging taghelpers in your application.






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I have created a paging taghelper for .net Core Razor Pages, it can be configured within html tags or appsettings.json to show/ hide prev-next, first-last buttons, number of max displayed pages and more customization settings are available,



                    The project is available to install as nuget package (and github):



                    Install-Package LazZiya.TagHelpers -Version 1.0.2


                    See installation steps here:
                    http://ziyad.info/en/articles/21-Paging_TagHelper_for_ASP_NET_Core



                    Live demo and all settings:
                    http://demo.ziyad.info/en/paging



                    Using appsettings.json for paging configurations will help to have more clean html code and it gives the ability to change paging settings on all or specific paging taghelpers in your application.






                    share|improve this answer












                    I have created a paging taghelper for .net Core Razor Pages, it can be configured within html tags or appsettings.json to show/ hide prev-next, first-last buttons, number of max displayed pages and more customization settings are available,



                    The project is available to install as nuget package (and github):



                    Install-Package LazZiya.TagHelpers -Version 1.0.2


                    See installation steps here:
                    http://ziyad.info/en/articles/21-Paging_TagHelper_for_ASP_NET_Core



                    Live demo and all settings:
                    http://demo.ziyad.info/en/paging



                    Using appsettings.json for paging configurations will help to have more clean html code and it gives the ability to change paging settings on all or specific paging taghelpers in your application.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 15 at 14:55









                    Laz Ziya

                    568310




                    568310






















                        up vote
                        0
                        down vote













                        I made this implementation mixing together a few answers on the subject, I hope it helps someone.



                        Add a PagedResultBase class (that you can extend adding other properties you need):



                        public abstract class PagedResultBase
                        {
                        public int CurrentPage { get; set; }
                        public int PageCount { get; set; }
                        public int PageSize { get; set; }
                        public int RowCount { get; set; }
                        }


                        Add a PagedResult class:



                        public class PagedResult<T> : PagedResultBase where T : class
                        {
                        public ICollection<T> Results { get; set; }

                        public PagedResult()
                        {
                        Results = new List<T>();
                        }
                        }


                        Add a IQueryableExtensions with a GetPagedResult extension:



                        public static class IQueryableExtensions
                        {
                        public async static Task<PagedResult<T>> GetPagedResultAsync<T>(this IQueryable<T> query, int currentPage, int pageSize) where T : class
                        {
                        var skip = (currentPage - 1) * pageSize;
                        var take = pageSize;

                        var rowCount = await query.CountAsync();
                        var results = await query.Skip(skip).Take(take).ToListAsync();

                        var pagedResult = new PagedResult<T> {
                        CurrentPage = currentPage,
                        PageCount = (int)Math.Ceiling(decimal.Divide(rowCount, pageSize)),
                        PageSize = pageSize,
                        RowCount = rowCount,
                        Results = results
                        };

                        return pagedResult;
                        }
                        }


                        You are done:



                        var pagedResult = await MyContext.Posts.Where(p => p.Featured == true).GetPagedResultAsync(1, 10);





                        share|improve this answer



























                          up vote
                          0
                          down vote













                          I made this implementation mixing together a few answers on the subject, I hope it helps someone.



                          Add a PagedResultBase class (that you can extend adding other properties you need):



                          public abstract class PagedResultBase
                          {
                          public int CurrentPage { get; set; }
                          public int PageCount { get; set; }
                          public int PageSize { get; set; }
                          public int RowCount { get; set; }
                          }


                          Add a PagedResult class:



                          public class PagedResult<T> : PagedResultBase where T : class
                          {
                          public ICollection<T> Results { get; set; }

                          public PagedResult()
                          {
                          Results = new List<T>();
                          }
                          }


                          Add a IQueryableExtensions with a GetPagedResult extension:



                          public static class IQueryableExtensions
                          {
                          public async static Task<PagedResult<T>> GetPagedResultAsync<T>(this IQueryable<T> query, int currentPage, int pageSize) where T : class
                          {
                          var skip = (currentPage - 1) * pageSize;
                          var take = pageSize;

                          var rowCount = await query.CountAsync();
                          var results = await query.Skip(skip).Take(take).ToListAsync();

                          var pagedResult = new PagedResult<T> {
                          CurrentPage = currentPage,
                          PageCount = (int)Math.Ceiling(decimal.Divide(rowCount, pageSize)),
                          PageSize = pageSize,
                          RowCount = rowCount,
                          Results = results
                          };

                          return pagedResult;
                          }
                          }


                          You are done:



                          var pagedResult = await MyContext.Posts.Where(p => p.Featured == true).GetPagedResultAsync(1, 10);





                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            I made this implementation mixing together a few answers on the subject, I hope it helps someone.



                            Add a PagedResultBase class (that you can extend adding other properties you need):



                            public abstract class PagedResultBase
                            {
                            public int CurrentPage { get; set; }
                            public int PageCount { get; set; }
                            public int PageSize { get; set; }
                            public int RowCount { get; set; }
                            }


                            Add a PagedResult class:



                            public class PagedResult<T> : PagedResultBase where T : class
                            {
                            public ICollection<T> Results { get; set; }

                            public PagedResult()
                            {
                            Results = new List<T>();
                            }
                            }


                            Add a IQueryableExtensions with a GetPagedResult extension:



                            public static class IQueryableExtensions
                            {
                            public async static Task<PagedResult<T>> GetPagedResultAsync<T>(this IQueryable<T> query, int currentPage, int pageSize) where T : class
                            {
                            var skip = (currentPage - 1) * pageSize;
                            var take = pageSize;

                            var rowCount = await query.CountAsync();
                            var results = await query.Skip(skip).Take(take).ToListAsync();

                            var pagedResult = new PagedResult<T> {
                            CurrentPage = currentPage,
                            PageCount = (int)Math.Ceiling(decimal.Divide(rowCount, pageSize)),
                            PageSize = pageSize,
                            RowCount = rowCount,
                            Results = results
                            };

                            return pagedResult;
                            }
                            }


                            You are done:



                            var pagedResult = await MyContext.Posts.Where(p => p.Featured == true).GetPagedResultAsync(1, 10);





                            share|improve this answer














                            I made this implementation mixing together a few answers on the subject, I hope it helps someone.



                            Add a PagedResultBase class (that you can extend adding other properties you need):



                            public abstract class PagedResultBase
                            {
                            public int CurrentPage { get; set; }
                            public int PageCount { get; set; }
                            public int PageSize { get; set; }
                            public int RowCount { get; set; }
                            }


                            Add a PagedResult class:



                            public class PagedResult<T> : PagedResultBase where T : class
                            {
                            public ICollection<T> Results { get; set; }

                            public PagedResult()
                            {
                            Results = new List<T>();
                            }
                            }


                            Add a IQueryableExtensions with a GetPagedResult extension:



                            public static class IQueryableExtensions
                            {
                            public async static Task<PagedResult<T>> GetPagedResultAsync<T>(this IQueryable<T> query, int currentPage, int pageSize) where T : class
                            {
                            var skip = (currentPage - 1) * pageSize;
                            var take = pageSize;

                            var rowCount = await query.CountAsync();
                            var results = await query.Skip(skip).Take(take).ToListAsync();

                            var pagedResult = new PagedResult<T> {
                            CurrentPage = currentPage,
                            PageCount = (int)Math.Ceiling(decimal.Divide(rowCount, pageSize)),
                            PageSize = pageSize,
                            RowCount = rowCount,
                            Results = results
                            };

                            return pagedResult;
                            }
                            }


                            You are done:



                            var pagedResult = await MyContext.Posts.Where(p => p.Featured == true).GetPagedResultAsync(1, 10);






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 at 16:13

























                            answered Nov 19 at 16:08









                            lolol

                            2,56622643




                            2,56622643






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50928891%2fhow-to-implement-pagination-in-asp-net-core-razor-pages%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                Refactoring coordinates for Minecraft Pi buildings written in Python