public async Task <ActionResult> Create([Bind(Include = "QuantityPerYear, MaterialName")] IEnumerable <OrderFormViewModel> viewModel) { if (ModelState.IsValid) { foreach (var item in viewModel) { var littleOffer = new OrderPerMaterial { //OrderID = item.OrderID, Material = db.Materials.Where(m => m.MaterialName == item.MaterialName).Single(), QuantityPerYear = item.QuantityPerYear }; db.OrderPerMaterials.Add(littleOffer); await db.SaveChangesAsync(); } return(RedirectToAction("Index")); } return(View(viewModel)); }
public async Task <ActionResult> Create([Bind(Include = "QuantityPerYear, MaterialName, MaterialID, OrderID, Price")] List <OrderFormViewModel> viewModel) { if (ModelState.IsValid) { foreach (var item in viewModel) { var littleOffer = new OrderPerMaterial { MaterialID = item.MaterialID, QuantityPerYear = item.QuantityPerYear, OrderID = item.OrderID, PricePerMaterial = item.Price }; db.OrderPerMaterials.Add(littleOffer); await db.SaveChangesAsync(); } return(RedirectToAction("Ok")); } return(View(viewModel)); }