public ActionResult Create(Product product, List<int> CategoryIds, List<int> TechnologyIds) { foreach (var id in CategoryIds) { var category = db.ProductCategories.Find(id); if (category != null) product.Categories.Add(category); } if (TechnologyIds != null) { foreach (var id in TechnologyIds) { var technology = db.Technologies.Find(id); if (technology != null) product.Technologies.Add(technology); } } if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Categories = db.ProductCategories.ToList(); ViewBag.SelectedCategories = product.Categories; CreateFrame(title); return View(product); }
// // GET: /AdminProduct/Create public ActionResult Create() { ViewBag.Categories = db.ProductCategories.ToList(); ViewBag.SelectedCategories = new List<ProductCategory>(); ViewBag.Technologies = db.Technologies.ToList(); ViewBag.SelectedTechnologies = new List<Technology>(); CreateFrame(title); var product = new Product(); product.Active = true; return View(product); }
private void UpdateProductsCategories(List<int> CategoryIds, Product product) { if (CategoryIds == null) { product.Categories = new List<ProductCategory>(); return; } var selectedCategories = new HashSet<int>(CategoryIds); var productCategories = new HashSet<int>( product.Categories.Select(c => c.Id)); foreach (var c in db.ProductCategories) { } }
public ActionResult Edit(Product product, List<int> CategoryIds, List<int> TechnologyIds) { using (var mdb = new VDB()) { var mproduct = mdb.Products .Include(i => i.Categories).Include(i => i.Technologies) .Where(i => i.Id == product.Id) .Single(); mproduct.Categories.Clear(); foreach (var id in CategoryIds) { var category = mdb.ProductCategories.Find(id); if (category != null) mproduct.Categories.Add(category); } mproduct.Technologies.Clear(); if (TechnologyIds != null) { foreach (var id in TechnologyIds) { var technology = mdb.Technologies.Find(id); if (technology != null) mproduct.Technologies.Add(technology); } } mdb.Entry(mproduct).State = EntityState.Modified; mdb.SaveChanges(); } if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Categories = db.ProductCategories.ToList(); ViewBag.SelectedCategories = product.Categories; CreateFrame(title); return View(product); /* if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } CreateFrame(title); return View(product); */ }