8.1.1 Filtering the Product List; Listing 8-2. Adding Category Support to the List Action Method in the ProductController.cs File

chap08
Jason Zhu 2021-09-08 21:25:39 +10:00
parent 0086a9a75f
commit 4b8ce0c014
1 changed files with 5 additions and 2 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
@ -18,11 +19,12 @@ namespace SportsStore.WebUI.Controllers
this.repository = productRepository; this.repository = productRepository;
} }
public ViewResult List(int page = 1) public ViewResult List(string category, int page = 1)
{ {
ProductsListViewModel model = new ProductsListViewModel ProductsListViewModel model = new ProductsListViewModel
{ {
Products = repository.Products Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID) .OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize) .Skip((page - 1) * PageSize)
.Take(PageSize), .Take(PageSize),
@ -31,7 +33,8 @@ namespace SportsStore.WebUI.Controllers
CurrentPage = page, CurrentPage = page,
ItemsPerPage = PageSize, ItemsPerPage = PageSize,
TotalItems = repository.Products.Count() TotalItems = repository.Products.Count()
} },
CurrentCategory = category
}; };
return View(model); return View(model);
} }