public ActionResult EditActiveProducts() { var userId = User.Identity.GetUserId(); var user = db.EcomUsers.FirstOrDefault(m => m.ApplicationUserId == userId); if (user is null) { return(View("Error")); } var prods = db.Products .Include(m => m.Category) .Include(m => m.Category.Store) .Where(m => m.Category.Store.EcomUserId == user.Id) .ToList(); var model = new ProductEditListedViewModel { Products = prods, Inventory = new Dictionary <Product, List <Stock> >(), IsActiveList = new Dictionary <int, bool>(), HasListing = new Dictionary <Product, bool>(), CurrentSelectedProducts = GetActiveProductsCount(), MaxAllowedProducts = GetMaxProductsAllowed() }; foreach (var prod in prods) { model.IsActiveList.Add(prod.Id, prod.IsActive); model.Inventory.Add(prod, db.Stocks .Include(m => m.Store) .Where(m => m.ProductId == prod.Id) .ToList()); if (db.Categories.FirstOrDefault(m => m.CategoryId == prod.CategoryId) == null) { model.HasListing.Add(prod, true); } else { model.HasListing.Add(prod, false); } } return(View(model)); }
public ActionResult EditActiveProducts(ProductEditListedViewModel model, string[] actives) { if (ModelState.IsValid) { var prodIds = Request.Form["prodIds"]; var activeProds = actives ?? new string[0]; if (actives == null) { actives = new string[0]; } var userId = User.Identity.GetUserId(); var user = db.EcomUsers.FirstOrDefault(m => m.ApplicationUserId == userId); if (user is null) { return(View("Error")); } var prods = db.Products .Include(m => m.Category) .Include(m => m.Category.Store) .Where(m => m.Category.Store.EcomUserId == user.Id) .ToList(); foreach (var prod in prods) { if (actives.Contains(prod.Id.ToString())) { prod.IsActive = true; } else { prod.IsActive = false; } db.Entry(prod).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("AllProducts")); } return(RedirectToAction("EditActiveProducts")); }