// GET: Catalouges/Details/5 public async Task <ActionResult> Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Catalouge catalouge = await db.Catalouges.FindAsync(id); if (catalouge == null) { return(HttpNotFound()); } //return View(supplier); CatalougeViewModel catalougeViewModel = new CatalougeViewModel(); catalougeViewModel.Catalouge = db.Catalouges.Include(c => c.Supplier).First(c => c.FoodId == id);; catalougeViewModel.AllSuppliers = db.Suppliers.Select(s => new SelectListItem { Text = s.SupplierName, Value = s.SupplierId.ToString() } ); catalougeViewModel.SelectedSuppliers = catalougeViewModel.Catalouge.Supplier.Select(s => s.SupplierId).ToList(); return(View(catalougeViewModel)); }
public async Task <ActionResult> Edit(CatalougeViewModel catalougeViewModel) { if (ModelState.IsValid) { Catalouge catalouge = db.Catalouges.Include(c => c.Supplier).First(c => c.FoodId == catalougeViewModel.Catalouge.FoodId); HashSet <int> selectedSuppliers = new HashSet <int>(catalougeViewModel.SelectedSuppliers); foreach (Supplier s in db.Suppliers) { if (!selectedSuppliers.Contains(s.SupplierId)) { catalouge.Supplier.Remove(s); } else { catalouge.Supplier.Add(s); } } db.Entry(catalouge).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(catalougeViewModel)); }