public ActionResult Index(string category = "All", int page = 1, int pageSize = 8) { Category cat = db.Categories.Where(x => x.name == category).FirstOrDefault(); List <Category> categories = db.Categories.ToList(); List <Product> products = null; if (cat == null) { products = db.Products.ToList(); } else { products = db.Products.Where(x => x.category_id == cat.id).ToList(); } PagedList <Product> pagedProduct = new PagedList <Product>(products, page, pageSize); CategoriesProducts model = new CategoriesProducts { activeCategory = category, categories = categories, products = pagedProduct }; return(View(model)); }
public IActionResult AddCatProd(int productID, int categoryID) { int?UserID = HttpContext.Session.GetInt32("UserID"); CategoriesProducts myCatProd = new CategoriesProducts(); myCatProd.productsid = productID; myCatProd.categoriesid = categoryID; _context.categoriesproducts.Add(myCatProd); _context.SaveChanges(); return(RedirectToAction("ProductType", new{ pid = productID })); }
protected HashSet <CategoriesProducts> AddProductsToCategories(Product[] products, Category[] categories) { var categoriesProducts = new HashSet <CategoriesProducts>(new CategoryProductsComparer()); for (int i = 0; i < products.Length; i++) { var category = categories[this.random.Next(0, categories.Length)]; var product = products[i]; var mapping = new CategoriesProducts(category, product); categoriesProducts.Add(mapping); var isThereFurtherCategories = this.random.Next() % 2 == 0; if (isThereFurtherCategories) { i--; } } return(categoriesProducts); }