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 (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { OrderInfo myOrder = new OrderInfo(); myOrder.Consignee = ReciverName; myOrder.Address = Address; myOrder.Phone = Phone; myOrder.Zip = PostCode; myOrder.CreateTime = DateTime.Now; myOrder.PaymentTime = DateTime.Now; myOrder.SendTime = null; myOrder.ReceiptTime = null; myOrder.UserName = ((Site1)Page.Master).CurrentUserName; context.AddToOrderInfoes(myOrder); context.SaveChanges(); var cartDetails = from p in context.CartDetails where p.CartInfo.UserName == ((Site1)Page.Master).CurrentUserName select p; foreach (CartDetail cartDetail in cartDetails) { OrderDetail orderDetail = new OrderDetail() { OrderId = myOrder.ID, ClothesId = cartDetail.ClothId.Value, QTY = cartDetail.QTY, UnitPrice = cartDetail.Cloth.Price, SizeId = cartDetail.SizeId.Value, ColorId = cartDetail.ColorId.Value }; myOrder.OrderDetails.Add(orderDetail); context.CartDetails.DeleteObject(cartDetail); } 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="clothesId">Initial value of the ClothesId property.</param> /// <param name="orderId">Initial value of the OrderId property.</param> /// <param name="colorId">Initial value of the ColorId property.</param> /// <param name="sizeId">Initial value of the SizeId 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 clothesId, global::System.Int32 orderId, global::System.Int32 colorId, global::System.Int32 sizeId, global::System.Int32 qTY, global::System.Decimal unitPrice) { OrderDetail orderDetail = new OrderDetail(); orderDetail.ID = id; orderDetail.ClothesId = clothesId; orderDetail.OrderId = orderId; orderDetail.ColorId = colorId; orderDetail.SizeId = sizeId; orderDetail.QTY = qTY; orderDetail.UnitPrice = unitPrice; return orderDetail; }