public IActionResult CustList(string CategoryName, string price, decimal number) { IEnumerable <Product> list = _productService.GetAll(); IEnumerable <Category> catList = _categoryService.GetAll(); ProductCustListViewModel vm = new ProductCustListViewModel { Products = list, //Products = HttpContext.Session.GetObjectFromJson<List<Product>>("customerList"), }; vm.Categories = new SelectList(catList, "CategoryId", "Name"); //if (!string.IsNullOrEmpty(CategoryName)) //{ // list = list.Where(l => l.Name == CategoryName); //} //if (!string.IsNullOrEmpty(price)) //{ // if (price == "max" && number != 0) // { // list = list.Where(l => l.Price <= number); // } // else if (price == "min" && number != 0) // { // list = list.Where(l => l.Price >= 0); // } //} return(View(vm)); }
public IActionResult CustList(ProductCustListViewModel vm) { IQueryable <Product> query = _productService.GetAll().AsQueryable(); IEnumerable <Category> catList = _categoryService.GetAll(); if (vm.MinPrice == 0 && vm.MaxPrice > 0) { query = query.Where(l => l.Price <= vm.MaxPrice); } if (vm.MinPrice > 0 && vm.MaxPrice == 0) { query = query.Where(l => l.Price >= vm.MinPrice); } if (vm.MinPrice > 0 && vm.MaxPrice > 0) { query = query.Where(l => l.Price > vm.MinPrice && l.Price <= vm.MaxPrice); } if (vm.CategoryId != 0) { query = query.Where(l => l.CategoryId == vm.CategoryId); } vm.Products = query.ToList(); vm.Categories = new SelectList(catList, "CategoryId", "Name", vm.CategoryId); //if (vm.CategoryId == vm.CategoryId) //{ // //} return(View(vm)); }