public ActionResult Edit([Bind(Include = "ID,Weight,Name,Type,Cost,ImageName")] Dish dishes, HttpPostedFileBase file) { List <int> ingr_list = new List <int>(); string ingr_str = ""; foreach (var ingr in Request.Form["Ingredients"].ToList()) { ingr_str += ingr; } string[] ingr_arr = ingr_str.Split(','); foreach (var str in ingr_arr) { ingr_list.Add(Convert.ToInt32(str)); } ingr_list.Remove(ingr_list.Last()); if (ModelState.IsValid) { if (file != null) { string fileName = System.IO.Path.GetFileName(file.FileName); file.SaveAs(Server.MapPath("~/Images/" + fileName)); db.Entry(dishes).State = EntityState.Modified; dishes.ImageName = "/Images/" + fileName; db.SaveChanges(); } else { db.Entry(dishes).State = EntityState.Modified; db.SaveChanges(); } int counter = 0; foreach (var pos_id in db.PositionsInDishes.Where(p => p.DishID == dishes.ID).Select(p => p.ID).ToList()) { IngredientsOnPosition ingredientsOnPosition = db.IngredientsOnPositions.Where(i => i.PositionInDishID == pos_id).First(); ingredientsOnPosition.IngredientID = ingr_list[counter]; db.Entry(ingredientsOnPosition).State = EntityState.Modified; db.SaveChanges(); counter++; } return(RedirectToAction("Index")); } ViewBag.Type = new SelectList(db.DishesTypes, "ID", "Type", dishes.Type); return(View(dishes)); }
public ActionResult Create([Bind(Include = "ID,Name,Type,Cost")] Dish dishes, HttpPostedFileBase file) { ViewData["Positions"] = new SelectList(db.Positions, "ID", "Name"); ViewData["Ingredients"] = new SelectList(db.Ingredients, "ID", "Name"); if (ModelState.IsValid && file != null && file.ContentLength > 0) { List <int> pos_list = new List <int>(); string pos_str = ""; foreach (var pos in Request.Form["Positions"].ToList()) { pos_str += pos; } string[] pos_arr = pos_str.Split(','); foreach (var str in pos_arr) { pos_list.Add(Convert.ToInt32(str)); } List <int> ingr_list = new List <int>(); string ingr_str = ""; foreach (var ingr in Request.Form["Ingredients"].ToList()) { ingr_str += ingr; } string[] ingr_arr = ingr_str.Split(','); foreach (var str in ingr_arr) { ingr_list.Add(Convert.ToInt32(str)); } foreach (var ingr in ingr_list) { double?wght = db.Ingredients.Where(i => i.ID == ingr).First().Weight; if (dishes.Weight == null) { dishes.Weight = wght; } else { dishes.Weight += wght; } } // extract only the fielname var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/Images"), fileName); file.SaveAs(path); dishes.ImageName = "/Images/" + fileName; db.Dishes.Add(dishes); db.SaveChanges(); for (int i = 0; i < pos_list.Count; i++) { PositionsInDish positionsInDish = new PositionsInDish(); IngredientsOnPosition ingredientsOnPosition = new IngredientsOnPosition(); positionsInDish.DishID = GetID(); positionsInDish.PositionID = pos_list[i]; db.PositionsInDishes.Add(positionsInDish); db.SaveChanges(); ingredientsOnPosition.PositionInDishID = positionsInDish.ID; ingredientsOnPosition.IngredientID = ingr_list[i]; db.IngredientsOnPositions.Add(ingredientsOnPosition); db.SaveChanges(); //double? wght = db.Ingredients.Find(ingredientsOnPosition.IngredientID).Weight; //dishes.Weight += wght; //db.Entry(dishes).State = EntityState.Modified; //db.SaveChanges(); } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Type = new SelectList(db.DishesTypes, "ID", "Type", dishes.Type); return(View(dishes)); }