Trying to create pagination in asp.net mvc

I'm trying to create pagination when I run this I expected to display 4 products in one page but I doesn't happen , it display all products in each categories ,so what wrong with that .

Controller

 public ActionResult Category(string name,int? page, int? catId)
        {

            //Declare List Of ProductVM
            List<ProductVM> listOfProductVM;
            //Set Page Number
            var pageNumber = page ?? 1;
            using (Db db = new Db())
            {

                //Get Category ID
                CategoryDTO categoryDTO = db.Categories.Where(x => x.Slug == name).FirstOrDefault();
                 catId = categoryDTO.Id;


                //Initialize The List
                        listOfProductVM = db.Products.ToArray()
                                         .Where(x => x.CategoryId == catId)
                                         .Select(x => new ProductVM(x))
                                         .ToList();


                //Get Category Name
                var productCat = db.Products.Where(x => x.CategoryId == catId).FirstOrDefault();
                ViewBag.CategoryName = productCat.CategoryName;


                //Populate Categories select list
                ViewBag.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                //set selected category
                ViewBag.SelectedCat = catId.ToString();
            }

            //set pagination
            var onePageOfProducts = listOfProductVM.ToPagedList(pageNumber, 4);
            ViewBag.OnePageOfProducts = onePageOfProducts;
            //return view with list

            return View(listOfProductVM);
        }

ScreenShoot This what display's

enter image description here

Could you please let me know where is problem !

1 answer

  • answered 2018-07-12 00:43 Fran

            //set pagination
            var onePageOfProducts = listOfProductVM.ToPagedList(pageNumber, 4);
            ViewBag.OnePageOfProducts = onePageOfProducts;
            //return view with list
    
            return View(listOfProductVM);
    

    Set the view's model to onePageOfProducts instead of to the entire list.

    But there's also other stuff that isn't optimal.

    You don't want to materialize the whole list like you do here:

                //Initialize The List
                        listOfProductVM = db.Products.ToArray()
                                         .Where(x => x.CategoryId == catId)
                                         .Select(x => new ProductVM(x))
                                         .ToList();
    

    remove the .ToArray() because that is going to load the entire table into memory and perform you Where clause in memory instead of converting the expression into a SQL statement. Also remove .ToList() so that you are returning an IQueryable instead of the entire materialized list.

    //Initialize The List
        var listOfProductVM = db.Products
            .Where(x => x.CategoryId == catId)
            .Select(x => new ProductVM
        {
        //set you properties individually
        VMProperty1 = x.Property1,
    
        });
    

    I'd also use DI and pass the Db() as a controller constructor parameter so that you aren't opening and closing it in the controller method.