public IActionResult CategoryProfile(int categoryId)
        {
            List <Products> AllProducts = dbContext.Products
                                          .ToList();

            @ViewBag.AllProducts = AllProducts;

            DateTime CurrentTime = DateTime.Now;

            ViewBag.Now = CurrentTime;


            Categories retrievedCategory = dbContext.Categories.SingleOrDefault(u => u.CategoriesId == categoryId);

            @ViewBag.retrievedCategory = retrievedCategory;



            AddProductToCategoryView newCategoryToProduct = new AddProductToCategoryView();

            newCategoryToProduct.Products = dbContext.Products
                                            .Include(c => c.Associated)
                                            .ThenInclude(a => a.Categories)
                                            .ToList();
            newCategoryToProduct.Categories = dbContext.Categories
                                              .Where(p => p.CategoriesId == categoryId)
                                              .FirstOrDefault();


            // AddProductToCategoryView newProductToCategory = new AddProductToCategoryView();


            // // Categories thisCategory = dbContext.Categories
            // //     .Where(c => c.CategoriesId == categoryId)
            // //     .Include(c => c.Associations)
            // //     .ThenInclude(assoc => assoc.Products)
            // //     .FirstOrDefault();

            // // newProductToCategory.Products = dbContext.Products
            // //     .Include(p => p.Associated).ToList();

            // // @ViewBag.AddProdToCategoryView = newProductToCategory;

            var categoryWithProducts = dbContext.Categories
                                       .Include(category => category.Associations)
                                       .ThenInclude(sub => sub.Products)
                                       .FirstOrDefault(category => category.CategoriesId == categoryId);

            @ViewBag.categoryWithProducts = categoryWithProducts;
            return(View());
        }
Example #2
0
        public IActionResult Show(int CategoryID)
        {
            AddProductToCategoryView AddProdToCategoryView = new AddProductToCategoryView();

            Category thisCategory = dbContext.Categories
                                    .Where(c => c.CategoryID == CategoryID)
                                    .Include(c => c.AssociatedProducts)
                                    .ThenInclude(assoc => assoc.Product)
                                    .FirstOrDefault();

            AddProdToCategoryView.Category = thisCategory;

            AddProdToCategoryView.Products = dbContext.Products
                                             .Include(p => p.AssociatedCategories).ToList();


            return(View(AddProdToCategoryView)); //AddProductToCategory
        }