运行此程序时,我试图创建分页,我希望在一页中显示4种产品,但我没有发生,它会显示每个类别中的所有产品
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);
}
答案 0 :(得分:2)
return View(onePageOfProducts);
代替
return View(listOfProductVM);