public PlatingAddList GetPlatingsUpdateList(int id) { var allPlatings = GetPlatings(); var service = new RecipeService(userId); var includedPlatings = service.GetPlatingsByRecipeId(id); var model = new PlatingAddList() { RecipeId = id, Platings = new List <PlatingAdd>() }; foreach (var plating in allPlatings) { model.Platings.Add( new PlatingAdd { ID = plating.ID, Name = plating.Name, Description = plating.Description }); } foreach (var plating in includedPlatings) { if (model.Platings.Any(p => p.ID == plating.ID)) { model.Platings.Single(p => p.ID == plating.ID).IsIncluded = true; } } return(model); }
public ActionResult AddUpdate(PlatingAddList model) { if (!ModelState.IsValid) { return(View(model)); } var service = CreatePlatingService(); if (service.AddPlating(model)) { TempData["Save Result"] = "Changes Saved."; return(RedirectToAction("Index", "Recipe", new { id = model.RecipeId })); } else { ModelState.AddModelError("", "Your plating style could not be update."); return(View(model)); } }
public PlatingAddList GetPlatingsAddList(int id) { var allPlatings = GetPlatings(); var model = new PlatingAddList() { RecipeId = id, Platings = new List <PlatingAdd>() }; foreach (var plating in allPlatings) { model.Platings.Add( new PlatingAdd { ID = plating.ID, Name = plating.Name, Description = plating.Description, IsIncluded = false }); } return(model); }
public bool AddPlating(PlatingAddList model) { using (var context = new CookbookContext()) { var entities = context.RecipePlatings.Where(e => e.RecipeID == model.RecipeId); foreach (var entity in entities) { context.RecipePlatings.Remove(entity); } foreach (var plating in model.Platings) { if (plating.IsIncluded == true) { context.Set <RecipePlating>().Add(new RecipePlating { PlatingID = plating.ID, RecipeID = model.RecipeId }); } } return(context.SaveChanges() != -1); } }