示例#1
0
        public ActionResult GetById(int id = 0, bool isOffer = false)
        {
            var app        = new ServiceReference.ContractClient();
            var categories = ViewBag.LayoutModel as List <CategoryViewModel>;
            List <ProductViewModel> productsToDisplay;

            if (id == 0)
            {
                if (isOffer)
                {
                    productsToDisplay = app.GetProductLastOffers(null).Select(x => new ProductViewModel(x)).ToList();
                }
                else
                {
                    productsToDisplay = app.GetAllProducts().Select(x => new ProductViewModel(x)).ToList();
                }
            }
            else
            {
                productsToDisplay = app.GetProductByCategory(id, isOffer).Select(x => new ProductViewModel(x)).ToList();
            }


            var categoriesAmount = app.GetCategoryCount(isOffer);

            foreach (var c in categories)
            {
                c.CountProduct = categoriesAmount.FirstOrDefault(x => x.Key == c.Id).Value;
            }


            foreach (var c in productsToDisplay)
            {
                c.Images = app.GetImageByProductId(c.Id);
            }
            var model = new CategoryShowViewModel
            {
                Products   = productsToDisplay,
                Categories = categories
            };

            if (id != 0)
            {
                model.CurrentCategory = categories.FirstOrDefault(x => x.Id == id);
            }


            return(View(model));
        }