我想显示不重复的类别名称和我的应用程序中给定类别中找到的产品数量。我能够看到我们搜索但无法处理产品数量的产品类别列表。类别列表位于变量中:EnableCategories例如:
搜索词组:华硕
PC(2)
笔记本电脑(1)
我的ViewModel:
public class ProductListViewModel
{
public IEnumerable<Category> Categories{ get; set; }
public IEnumerable<Product> EnableCategories { get; set; }
public int CountProduct { get; set; }
public IEnumerable<Product> SaleProduct { get; set; }
public Product DetailsProduct { get; set; }
public IPagedList<Product>Products{ get; set; }
public string CurrentCategory{ get; set; }
}
我的控制器:
public ActionResult SearchProduct(string currentFilter, string searchString, int? page)
{
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
int pageSize = 1;
int pageNumber = page ?? 1;
var products = productRepository.Products.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())).ToList();
var categories = products.Where(s => s.CategoryID == s.Category.CategoryID).ToList();
var vm = new ProductListViewModel()
{
EnableCategories = categories,
Products = products.ToPagedList(pageNumber, pageSize)
};
return View(vm);
}
和查看:
@model TechCom.App.Models.ProductListViewModel
@using PagedList.Mvc;
@using PagedList;
@using System.Linq
@{
ViewBag.Title = "SearchProduct";
}
@foreach (var item in Model.EnableCategories)
{
<a href="@Url.Action("ProductList","Product", new{
categoryName=item.Category.CategoryName,currentFilter =
ViewBag.CurrentFilter})">
@item.Category.CategoryName </a>
<br />
}
@foreach (var item in Model.Products)
{
@Html.Partial("_ProductList", item)
}
@Html.PagedListPager(Model.Products, page => Url.Action("SearchProduct", new { page, currentFilter = ViewBag.CurrentFilter }))