public void Create( IngredientsViewModel ingredient) { if (ModelState.IsValid) { Ingredient item = new Ingredient { CategoryID = Convert.ToInt16(ingredient.CategoryName), IngredientID = (db.Ingredient.Max(x => x.IngredientID)+1), Title = ingredient.Title, LongDescription = ingredient.LongDescription, Cost = ingredient.Cost, Weight = ingredient.Weight }; db.Ingredient.Add(item); db.SaveChanges(); if (ingredient.SubIngredient != null) foreach (Tmodel t in ingredient.SubIngredient) { ProductIngredient subIngredient = new ProductIngredient(); subIngredient.ProductID = item.IngredientID; subIngredient.IngredientID = t.id; subIngredient.ProductIngredientID = (db.ProductIngredient.Max(x => x.ProductIngredientID) + 1); db.ProductIngredient.Add(subIngredient); db.SaveChanges(); } } // действия по добавлению }
public void Edit( IngredientsViewModel ingredient) { Ingredient item = new Ingredient { CategoryID = Convert.ToInt16(ingredient.CategoryName), IngredientID = ingredient.IngredientID, Title = ingredient.Title, LongDescription = ingredient.LongDescription, Cost = ingredient.Cost, Weight = ingredient.Weight }; if (ModelState.IsValid) { db.Entry(item).State = EntityState.Modified; db.SaveChanges(); if (ingredient.SubIngredient != null) foreach (Tmodel t in ingredient.SubIngredient) { ProductIngredient subIngredient = new ProductIngredient(); subIngredient.ProductID = item.IngredientID; subIngredient.IngredientID = t.id; subIngredient.ProductIngredientID = (db.ProductIngredient.Max(x => x.ProductIngredientID) + 1); db.ProductIngredient.Add(subIngredient); db.SaveChanges(); } } }