コード例 #1
0
ファイル: CartsController.cs プロジェクト: ngobakhanh/Shoes
        public ActionResult CheckOut()
        {
            Order         order = null;
            SHOESEntities db    = new SHOESEntities();

            using (var tran = db.Database.BeginTransaction())
            {
                try
                {
                    string   user_id  = User.Identity.GetUserId();
                    Customer customer = db.Customers.Where(c => c.user_id == user_id).First();
                    Employee employee = db.Employees.Find(1);
                    order = new Order
                    {
                        date     = DateTime.Now,
                        status   = "New",
                        Customer = customer,
                        Employee = employee
                    };
                    db.Orders.Add(order);
                    IList <Item> items = Cart.Items;
                    foreach (var item in items)
                    {
                        Product     product     = db.Products.Find(item.ID);
                        OrderDetail orderdetail = new OrderDetail
                        {
                            order_id = order.id,
                            Product  = product,
                            quantity = item.Quantity,
                            price    = item.Price,
                            discount = item.Discount
                        };
                        db.OrderDetails.Add(orderdetail);
                    }
                    db.SaveChanges();
                    Cart.Empty();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
            return(View(order));
        }