public ActionResult Add(Models.OrderLine ol) { //see if the product is already present // in our cart if (this.MyOrder.OrderLines.Where(x => x.ProductID == ol.ProductID).Any()) { //get the order line to update the quantity on var cartItem = this.MyOrder.OrderLines.Where(x => x.ProductID == ol.ProductID).First(); //update the quantity cartItem.Quantity = cartItem.Quantity + ol.Quantity; } else { //not in cart, add our orderline to our order this.MyOrder.OrderLines.Add(ol); } //save changes db.SaveChanges(); //return the MiniCart View return(RedirectToAction("MiniCart", "Cart")); }
public ActionResult Product(int id, FormCollection values) { int qty = int.Parse(values["qty"]); int orderID = GetOrderID(); //Get our oorder db.Orders.Find(orderID); //Make a new order line var orderLine = new Models.OrderLine(); orderLine.ProductID = id; orderLine.Quantity = qty; orderLine.UnitPrice = decimal.Parse(values["price"]); orderLine.OrderID = orderID; //Add the order line to the database db.OrderLines.Add(orderLine); db.SaveChanges(); var product = db.Products.Find(id); ViewBag.SuccessMessage = "Added " + qty + " to the cart."; return View(product); }