public ActionResult Products(int?id) { List <ProductVM> products = new List <ProductVM>(); if (TempData["SearchResult"] != null) { foreach (Product item in TempData["SearchResult"] as List <Product> ) { ProductVM productVM = new ProductVM(); productVM.Product = item; productVM.Images = imageFactory.GetBy("ProductID", item.ID); productVM.Category = categoryFactory.Get(item.CategoryID); products.Add(productVM); } } else if (id == null) { foreach (Product item in productFactory.GetAll()) { ProductVM productVM = new ProductVM(); productVM.Product = item; productVM.Images = imageFactory.GetBy("ProductID", item.ID); productVM.Category = categoryFactory.Get(item.CategoryID); products.Add(productVM); } } else { foreach (Product item in productFactory.GetBy("CategoryID", id)) { ProductVM productVM = new ProductVM(); productVM.Product = item; productVM.Images = imageFactory.GetBy("ProductID", item.ID); productVM.Category = categoryFactory.Get(item.CategoryID); products.Add(productVM); } } return(View(products)); }