Esempio n. 1
0
        protected void ButtonConfirm_Click(object sender, EventArgs e)
        {
            //删除所有购物车明细,创建订单,创建订单明细。
            //一个字符串对象,保存用户名
            String userName = ((Site1)Page.Master).CurrentUserName;
            if (userName.Equals(""))
                return;
            if(TextBox1.Text.Trim().Length == 0
                || TextBox2.Text.Trim().Length == 0
                || TextBox3.Text.Trim().Length == 0
                || TextBox4.Text.Trim().Length == 0)
            {
                Response.Write("<script>alert('请输入所有资料');</script>");
            }
            String ReciverName = TextBox1.Text.Trim();
            String Address = TextBox2.Text.Trim();
            String Phone = TextBox3.Text.Trim();
            String PostCode = TextBox4.Text.Trim();
            using (ToyStoreEntities context = new ToyStoreEntities())
            {
                Order myOrder = new Order();
                myOrder.ReciverName = ReciverName;
                myOrder.Address = Address;
                myOrder.Phone = Phone;
                myOrder.PostCode = PostCode;
                myOrder.CreateTime = DateTime.Now;
                myOrder.PayTime = DateTime.Now;
                myOrder.UserName = ((Site1)Page.Master).CurrentUserName;
                context.AddToOrders(myOrder);
                context.SaveChanges();
                var cartDetails = from p in context.CartDetails
                                  where p.Cart.UserName == ((Site1)Page.Master).CurrentUserName
                                  select p;
                foreach (CartDetail cartDetail in cartDetails)
                {
                    OrderDetail orderDetail = new OrderDetail()
                    {
                        OrderId = myOrder.Id,
                        ProductId = cartDetail.ProductId,
                        Qty = cartDetail.OTY,
                        UnitPrice = cartDetail.Product.Price
                    };
                    myOrder.OrderDetails.Add(orderDetail);
                    context.CartDetails.DeleteObject(cartDetail);
                    orderDetail.Product.StockQty -= cartDetail.OTY;
                }

                context.SaveChanges();
                //新建一个OrderInfo类,即创建一个订单
                Response.Redirect(String.Format("~/ViewOrderDetail.aspx?ID={0}", myOrder.Id));
            }
        }