public ActionResult Index(Product model, int?quantity) { Order order = CurrentOrder; OrderedProduct orderedProduct = order.OrderedProducts.FirstOrDefault(x => x.SKU == model.SKU); if (orderedProduct != null) { orderedProduct.Quantity += quantity ?? 1; } else { orderedProduct = new OrderedProduct { SKU = model.SKU, Quantity = quantity ?? 1, ProductPrice = model.Price, Weight = model.Weight }; order.OrderedProducts.Add(orderedProduct); } order.LastModifiedOn = DateTime.UtcNow; entities.SaveChanges(); return(RedirectToAction("Index", "Cart")); }
public ActionResult Index(Order model) { var order = entities.Orders.Find(model.OrderID); foreach (var product in order.OrderedProducts) { var modelProduct = model.OrderedProducts.FirstOrDefault(x => x.SKU == product.SKU); product.Quantity = modelProduct.Quantity; } order.LastModifiedOn = DateTime.UtcNow; entities.OrderedProducts.RemoveRange(order.OrderedProducts.Where(x => x.Quantity == 0)); entities.SaveChanges(); return(RedirectToAction("Index")); }