title: C# Changing stock quantity with ordered
body: <p>After placing an order how would I then use the Quantity from the ShoppingCart to adjust the quantity from the Product in the database, lowering the stock available for purchase?</p>
  
  <p>Here is my classes,
  ShoppingCart Class with Create Order</p>
  
  <pre><code> public int CreateOrder(Order order)
      {
          decimal orderTotal = 0;
  
          var cartItems = GetCartItems();
  
          // Iterate over the items in the cart, adding the order details for each
          foreach (var item in cartItems)
          {
              var orderDetail = new OrderDetail
              {
                  ProductId = item.ProductId,
                  OrderId = order.OrderId,
                  UnitPrice = item.Product.Price,
                  Quantity = item.Count
              };
              // Set the order total of the shopping cart
              orderTotal += (item.Count * item.Product.Price);
  
              storeDB.OrderDetails.Add(orderDetail);
  
          }
          // Set the order's total to the orderTotal count
          order.Total = orderTotal;
  
          // Save the order
          storeDB.SaveChanges();
          // Empty the shopping cart
          EmptyCart();
          // Return the OrderId as the confirmation number
          return order.OrderId;
      }
  </code></pre>
  
  <p>My Order Class</p>
  
  <pre><code>[Bind(Exclude = "OrderId")]
  public partial class Order
  {
      [ScaffoldColumn(false)]
      public int OrderId { get; set; }
  
      [ScaffoldColumn(false)]
      public System.DateTime OrderDate { get; set; }
  
      [ScaffoldColumn(false)]
      [Remote("CheckUserName", "Account")]
      public string Username { get; set; }
  
      [Required]
      [StringLength(16, ErrorMessage = "Your name is too long")]
      [Display(Name = "First Name")]
      public string FirstName { get; set; }
  
      [Required(ErrorMessage = "Your last name is required.")]
      [StringLength(16, ErrorMessage = "Last name is too long.")]
      [Display(Name = "Last Name")]
      public string LastName { get; set; }
  
      [Required(ErrorMessage = "Address is required.")]
      public string Address { get; set; }
  
      [Required(ErrorMessage = "City is required.")]
      public string City { get; set; }
  
      [Required(ErrorMessage = "Postcode is required.")]
      [Display(Name = "Post Code")]
      public string PostalCode { get; set; }
  
      [Required(ErrorMessage = "Country is required.")]
      public string Country { get; set; }
  
      [Required(ErrorMessage = "Phone number is required.")]
      public string Phone { get; set; }
  
      [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email doesn't look like a valid email address.")]
      public string Email { get; set; }
  
      [System.ComponentModel.DataAnnotations.Compare("Email")]
      [Display(Name = "Confirm your email address")]
      public string EmailConfirm { get; set; }
  
  
      [ScaffoldColumn(false)]
      public string PaymentTransactionId { get; set; }
  
  
      [ScaffoldColumn(false)]
      public bool HasBeenShipped { get; set; }
  
      [ScaffoldColumn(false)]
      [ReadOnly(true)]
      public decimal Total { get; set; }
  
      public CardDetails cardDetails { get; set; }
      //public List&lt;CardDetails&gt; cardDetails { get; set; }
      public List&lt;OrderDetail&gt; OrderDetails { get; set; }
  }
  </code></pre>
  
  <p>My CheckOutController</p>
  
  <pre><code>[HttpPost]
          public ActionResult AddressAndPayment(FormCollection values)
          {
              var order = new Order();
              TryUpdateModel(order);
  
              //try
              //{
                  //if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false)
                  //{
                  //    return View(order);
                  //}
                  //else
                  //{
                      order.Username = User.Identity.Name;
                      order.OrderDate = DateTime.Now;
  
                      //Order gets saved
                      storeDB.Orders.Add(order);
                      storeDB.SaveChanges();
                      //Order gets processed
                      var cart = ShoppingCart.GetCart(this.HttpContext);
                      cart.CreateOrder(order);
  
                      //NEW TEST IF SAVES
                      storeDB.SaveChanges();
  
                      //Model.Product.stock = item.Product.stock - item.count;
  
  
  
                      return RedirectToAction("Complete",
                          new { id = order.OrderId });
  </code></pre>
  
  <p>OrderDetails Class</p>
  
  <pre><code>public class OrderDetail
  {
      public int OrderDetailId { get; set; }
      public int OrderId { get; set; }
      public int ProductId { get; set; }
      public int Quantity { get; set; }
      public decimal UnitPrice { get; set; }
      public virtual Product Product { get; set; }
      public virtual Order Order { get; set; }
  }
  </code></pre>