public ActionResult ListProducts(int?page, int?itemsPerPage, string sortOrder, string currentFilter, string searchString) { if (!isAdmin()) { return(RedirectToAction("LogIn", "Main")); } ViewBag.CurrentSort = sortOrder; ViewBag.ItemSortParm = String.IsNullOrEmpty(sortOrder) ? "item_desc" : ""; ViewBag.NameSortParm = sortOrder == "Name" ? "name_desc" : "Name"; ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price"; ViewBag.ProducerSortParm = sortOrder == "Producer" ? "producer_desc" : "Producer"; ViewBag.VolumeSortParm = sortOrder == "Volume" ? "volume_desc" : "Volume"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; List <Product> allProducts; if (!String.IsNullOrEmpty(searchString)) { allProducts = _product.getResult(searchString); } else { allProducts = _product.getAll(); } switch (sortOrder) { case "item_desc": allProducts = allProducts.OrderByDescending(s => s.itemnumber).ToList(); break; case "name_desc": allProducts = allProducts.OrderByDescending(s => s.name).ToList(); break; case "Name": allProducts = allProducts.OrderBy(s => s.name).ToList(); break; case "Price": allProducts = allProducts.OrderBy(s => s.price).ToList(); break; case "price_desc": allProducts = allProducts.OrderByDescending(s => s.price).ToList(); break; case "Producer": allProducts = allProducts.OrderBy(s => s.producer).ToList(); break; case "producer_desc": allProducts = allProducts.OrderByDescending(s => s.producer).ToList(); break; case "Volume": allProducts = allProducts.OrderBy(s => s.volum).ToList(); break; case "volume_desc": allProducts = allProducts.OrderByDescending(s => s.volum).ToList(); break; default: allProducts = allProducts.OrderBy(s => s.itemnumber).ToList(); break; } ViewBag.CurrentItemsPerPage = itemsPerPage; List <ProductInfo> list = new List <ProductInfo>(); foreach (var item in allProducts) { list.Add( new ProductInfo() { itemnumber = item.itemnumber, name = item.name, description = item.description, category = item.category, subCategory = item.subCategory, country = item.country, price = item.price, producer = item.producer, volum = item.volum, longDescription = item.longDescription, pricePerLitre = item.pricePerLitre }); } return(View(list.ToPagedList(pageNumber: page ?? 1, pageSize: itemsPerPage ?? 15))); }