public FoodViewsModel GetFoodViewsModel(Food food, string id) { var Results = from ingredient in db.foodIngredients select new { ingredient.Ingredient, Checked = ((from Fingredient in db.FoodtoFoodIngredients where (Fingredient.FoodName == id) & (Fingredient.IngredientId == ingredient.Ingredient) select Fingredient).Count() > 0) }; var NewFoodViewModel = new FoodViewsModel(); NewFoodViewModel.FoodName = id; NewFoodViewModel.Price = food.Price; NewFoodViewModel.FoodType = food.FoodType; NewFoodViewModel.Quantity = food.Quantity; NewFoodViewModel.ImageLink = food.ImageLink; var MyCheckBoxList = new List <CheckBoxViewModel>(); foreach (var ingredient in Results) { MyCheckBoxList.Add(new CheckBoxViewModel { Name = ingredient.Ingredient, Checked = ingredient.Checked }); } NewFoodViewModel.FoodIngredients = MyCheckBoxList; return(NewFoodViewModel); }
public ActionResult Edit(FoodViewsModel food) { if (ModelState.IsValid) { var Food = db.foods.Find(food.FoodName); Food.FoodName = food.FoodName; Food.Price = food.Price; Food.Quantity = food.Quantity; Food.FoodType = food.FoodType; Food.ImageLink = food.ImageLink; foreach (var ingredient in db.FoodtoFoodIngredients) { if (ingredient.FoodName == food.FoodName) { db.Entry(ingredient).State = System.Data.Entity.EntityState.Deleted; } } Food.FoodIngredients = "("; foreach (var ingredient in food.FoodIngredients) { if (ingredient.Checked) { db.FoodtoFoodIngredients.Add(new FoodtoFoodIngredients { FoodName = food.FoodName, IngredientId = ingredient.Name, } ); Food.FoodIngredients += ingredient.Name + ","; } } Food.FoodIngredients += ")"; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(food)); }