Using PagedList for paginating product list

Viewed 279

I am trying to show a list of products and show pagination. So, I decided to use PagedList plugin. I read some examples of how to modify controller and view to do so in the link below. https://github.com/TroyGoode/PagedList

But, instead of using ViewBag, I managed to send a group of products to controller by doing some modifications to controller:

public ActionResult Index(string name)
{
    IEnumerable<ProductModel> productList = new HashSet<ProductModel>();

    if (string.IsNullOrWhiteSpace(name))
    {

    }
    else
    {
        CategoryModel category = db.Categories.Where(x => x.CategorytUrl == name).FirstOrDefault();
        if (category != null)
        {
            int catId = category.Id;
            string catName = category.Name;
            ViewBag.categoryName = category.Name;
            List<SpecItemsModel> options = Products.GetFilter(category.Id);
            //initialize the list
            //productList = Products.ProductLists(catId);
            ViewBag.filters = options;
            var childrenIDs = db.Categories.Where(x => x.ParentId == category.Id).Select(x => x.Id);
            var grandchildrenIDs = db.Categories.Where(x => childrenIDs.Contains((int)x.ParentId)).Select(x => x.Id);
            List<int> catIds = new List<int>();
            catIds.Add(category.Id);
            catIds.AddRange(childrenIDs);
            catIds.AddRange(grandchildrenIDs);

            var templates = db.ProductTemplates.Where(t => catIds.Contains(t.CategoryId)).Select(x => x.Id);
            productList = db.Products.Where(p => templates.Contains(p.ProductTemplateId));
        }
    }
                var pageNumber = page ?? 1;
        //var onePageOfProducts = productList.OrderBy(i => i.AdminRate).ToPagedList(pageNumber, 2);
        //ViewBag.OnePageOfProducts = onePageOfProducts;
        return View(productList.OrderBy(i => i.AdminRate).ToPagedList(pageNumber, 2));
}

As the result, products are showing up in he view in 2-product groups. the only thing I don't know is how to modify html pager of the example I mentioned to show paging controls. :

    @model IEnumerable<fardashahr.Models.ProductModel>

    <div class="col-md-9">

        @foreach (var item in Model)
        {
                    <a href="@Url.Action("Details","Home",new { name = item.ProductUrl })">
                        <img class="bd-placeholder-img card-img-top mw-100" src="/Images/Uploads/Products/@item.Id/Thumbs/@item.ImageName" />
                    </a>
                        <p class="card-text">
                            <h3>
                                @item.ShortName
                            </h3>
                        </p>
        }
    </div>
</div>

Any suggestions?

1 Answers

Sorry, I didn't check the github project you posted.

In Controller

Change

public ActionResult Index(string name)

to

public ActionResult Index(int page, string name)

In View

Use

@Html.PagedListPager( (IPagedList)Model, page => Url.Action("Index", new { page = page, name = "***" }) )

to show page navigator.

Related