public ActionResult UpdateQuantity(int orderID, int orderDetailID, int amount) { string email = User.Identity.GetUserName(); //Get the order detail OrderDetail orderDetail = db.OrderDetails .Where(j => j.OrderDetailId == orderDetailID) .FirstOrDefault(); //Get the product information Product product = db.Products .Where(i => i.ProductId == orderDetail.ProductId) .FirstOrDefault(); //Update the quantity and the total int previousQuantity = orderDetail.Quantity; orderDetail.Quantity = orderDetail.Quantity + amount; orderDetail.Quantity = ((orderDetail.Quantity <= 0) ? 1 : orderDetail.Quantity); //Dont let the quantity go below 1 int newQuantity = orderDetail.Quantity; orderDetail.Quantity = ((orderDetail.Quantity > product.Stock) ? previousQuantity : newQuantity); //Dont let the quantity go above the stock of the product orderDetail.Total = product.DiscountedPrice * orderDetail.Quantity; db.Entry(orderDetail).State = EntityState.Modified; //Cart. Go through and update the cart total Order cart = db.Orders.FirstOrDefault(i => i.Email == email); db.Entry(cart).State = EntityState.Modified; if (previousQuantity != newQuantity) { if (amount < 0) { cart.Total -= (product.DiscountedPrice * Math.Abs(amount)); } else { cart.Total += (product.DiscountedPrice * Math.Abs(amount)); } } db.SaveChanges(); return(Redirect("/Orders/Details/" + orderID)); }
public ActionResult Edit([Bind(Include = "OrderDetailId,ProductId,Quantity,Total")] OrderDetail orderDetail) { if (ModelState.IsValid) { db.Entry(orderDetail).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(orderDetail)); }
public ActionResult Edit([Bind(Include = "ProductId,Name,Description,Price,DiscountedPrice,Stock,Purchases,Picture,Type,DateCreated,Featured")] Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }