我创建了一个下拉列表来过滤结果。第一次这很好用,但是如果用户选择了另一个选项,它会过滤过滤后的结果。有没有办法在再次过滤之前清除结果?
剃刀
@using (Html.BeginForm("Index", "Admin", FormMethod.Get))
{
@Html.DropDownList("category", (SelectList)ViewBag.Categories, "--Select One--")
<input type="submit" value="Filter" />
}
服务器代码
public ViewResult Index(string category, int page = 1)
{
var categoryList = categoriesRepository.Categories.Select(c => c.CategoryName).Distinct();
ViewBag.Categories = new SelectList(categoryList.AsEnumerable());
var viewModel = new ProductsListViewModel
{
Products = repository.Products
.Where(p => category == null || p.Category == category)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null
? repository.Products.Count()
: repository.Products.Where(e => e.Category == category).Count()
},
CurrentCategory = category
};
return View(viewModel);
}