1

I've created a dropdown to filter results. This works great the 1st time, but if the user selects another option it filters on the filtered results. Is there a way to clear the results before it filters again?

RAZOR

@using (Html.BeginForm("Index", "Admin", FormMethod.Get))
{
  @Html.DropDownList("category", (SelectList)ViewBag.Categories, "--Select One--") 
  <input type="submit" value="Filter" /> 
}

Server code

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);
}
4

0 回答 0