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)); } }
/// <summary> /// Deprecated Method for adding a new object to the OrderDetails EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToOrderDetails(OrderDetail orderDetail) { base.AddObject("OrderDetails", orderDetail); }
/// <summary> /// Create a new OrderDetail object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="orderId">Initial value of the OrderId property.</param> /// <param name="productId">Initial value of the ProductId property.</param> /// <param name="qty">Initial value of the Qty property.</param> /// <param name="unitPrice">Initial value of the UnitPrice property.</param> public static OrderDetail CreateOrderDetail(global::System.Int32 id, global::System.Int32 orderId, global::System.Int32 productId, global::System.Int32 qty, global::System.Decimal unitPrice) { OrderDetail orderDetail = new OrderDetail(); orderDetail.Id = id; orderDetail.OrderId = orderId; orderDetail.ProductId = productId; orderDetail.Qty = qty; orderDetail.UnitPrice = unitPrice; return orderDetail; }