public ActionResult CreateAlcohol(CreateAlcoholVM model, List<String> AssociatedReports) { var db = new ChwinockEntities(); var newAlcohol = new Alcohol(); newAlcohol.AlcoholID = Guid.NewGuid(); newAlcohol.Name = model.Alcohol.Name; newAlcohol.Price = model.Alcohol.Price; newAlcohol.Percentage = model.Alcohol.Percentage; newAlcohol.Type = model.Alcohol.Type; newAlcohol.Thoughts = model.Alcohol.Thoughts; if (AssociatedReports != null) { foreach (var r in AssociatedReports) { var id = Guid.Parse(r); newAlcohol.Reports.Add(db.Reports.Single(x => x.ReportID == id)); } } db.Alcohols.AddObject(newAlcohol); db.SaveChanges(); return RedirectToAction("Manage"); }
public ActionResult EditAlcohol(CreateAlcoholVM model, List<String> AssociatedReports) { var db = new ChwinockEntities(); var targetAlcohol = db.Alcohols.Single(r => r.AlcoholID == model.Alcohol.AlcoholID); targetAlcohol.Name = model.Alcohol.Name; targetAlcohol.Type = model.Alcohol.Type; targetAlcohol.Price = model.Alcohol.Price; targetAlcohol.Percentage = model.Alcohol.Percentage; targetAlcohol.Thoughts = model.Alcohol.Thoughts; // Remove associated reports List<Report> reportsToRemove = new List<Report>(); foreach (var r in targetAlcohol.Reports) { reportsToRemove.Add(r); } foreach (var r in reportsToRemove) { targetAlcohol.Reports.Remove(r); } if (AssociatedReports != null) { // Add new ones foreach (var r in AssociatedReports) { var id = Guid.Parse(r); targetAlcohol.Reports.Add(db.Reports.Single(x => x.ReportID == id)); } } db.SaveChanges(); return RedirectToAction("Manage"); }