EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

[SOLVED]How to use X.PagedList.Mvc.Core IN .NET Core 3.1?

Background code:

public async Task<IActionResult> Index(int? page,string search)
{
    var applicationDbContext = _context.Blogs.Include(b => b.Context).Include(b => b.Owner).Include(b => b.Type).OrderByDescending(x=>x.PublishTime);
    if (search != null)
    {
        var products = applicationDbContext.Where(x => x.Title.Contains(search)); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?

        var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
        var onePageOfProducts = products.ToPagedList(pageNumber, 10); // will only contain 10 products max because of the pageSize

        ViewBag.OnePageOfProducts = onePageOfProducts;
        return View(await onePageOfProducts.ToListAsync());

    }
    else
    {
        var products = applicationDbContext; //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?

        var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
        var onePageOfProducts = products.ToPagedList(pageNumber, 10); // will only contain 10 products max because of the pageSize

        ViewBag.OnePageOfProducts = onePageOfProducts;
        return View(await onePageOfProducts.ToListAsync());

    }

}

Razor page code:

@model IEnumerable<BlogPlatform.Models.Blog>
@using X.PagedList.Mvc.Core;
@using X.PagedList;


<form action="~/Blogs/Index" method="post">
    <p>
        Title: <input type="text" name="search" />
        <input type="submit" value="search" />
    </p>
</form>
……
@foreach (var item in Model)
{
……
}

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }))

Let me first try to explain the background C# code:

This is a typical rewritten method.

Its initial version should look like this:

public async Task<IActionResult> Index()
{
    return View(await _context.Users.ToListAsync());
}

Why does the rewritten code become so complicated?

Because it not only has a paging function, it also comes with a search function.

Every time Index is opened, it will request the else part first. The first thing is to get the data context, and then see if there is a page parameter, if not, the result of the first page will be displayed by default.

Then, you may see the core part:

var onePageOfProducts = products.ToPagedList(pageNumber, 10); // will only contain 10 products max because of the pageSize
ViewBag.OnePageOfProducts = onePageOfProducts;
return View(await onePageOfProducts.ToListAsync());

It corresponds to the last line of the razor page I mentioned above:

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Index", new { page }))

So, we can extract the core, the part you should modify is:

@Html.PagedListPager({Your ViewBag}, page => Url.Action("Index", new {page}))

In this way, you have completed the paging function and the search function.
But there is another problem, that is, the paging function after the search is not completed. I leave this task to you to figure out how to do it, because it is better to teach people how to fish.

This article was last edited at 2020-10-29 01:02:03

* *