public ActionResult Create() { using (var context = new MyContext()) { var bundle = new Bundle(); bundle.BannerUrl = "~/Content/img/default.jpg"; var products = context.Products.ToList(); return View(new BundleVM { Bundle = bundle, Products = products }); } }
private void SaveBundle(BundleVM bundleVM) { Bundle bundle; using (var context = new MyContext()) { if(bundleVM.Bundle.Id == 0){ bundle = new Bundle(); context.Bundles.Add(bundle); } else{ bundle = context.Bundles.Include("Products") .First(b => b.Id == bundleVM.Bundle.Id); } bundle.Name = bundleVM.Bundle.Name; bundle.Description = bundleVM.Bundle.Description; bundle.RiotPoints = bundleVM.Bundle.RiotPoints; bundle.BannerUrl = bundleVM.Bundle.BannerUrl; bundle.Products = bundleVM.ProductIds.Select(pi => context.Products.Find(pi)).ToList(); context.SaveChanges(); } }