示例#1
0
        public ActionResult CartToOrder()
        {
            //当前用户
            var customer = Session["user"] as Customer;

            //当前用户的购物车
            var cart = bse.Carts.Where(c => c.customerId == customer.id).FirstOrDefault();

            //为当前用户创建一个订单
            Models.Order order = new Models.Order()
            {
                num             = cart.num,
                price           = cart.price,
                customer        = cart.customerId,
                ordernum        = Guid.NewGuid().ToString(),
                status          = 0,
                receiverAddress = customer.address,
                receiverName    = customer.username,
                receiverPhone   = customer.telephone
            };
            order = bse.Orders.Add(order);

            //为订单添加明细
            foreach (var item in cart.CartItems)
            {
                var orderItem = new Models.OrdersItem()
                {
                    num      = item.num,
                    price    = item.price,
                    bookId   = item.bookId,
                    ordersId = order.id
                };
                order.OrdersItems.Add(orderItem);
            }

            //清空购物车
            bse.Carts.Remove(cart);

            bse.SaveChanges();

            return(Redirect("~/Home/OrderInfo?orderId=" + order.id));
        }
示例#2
0
        public ActionResult CartToOrder()
        {
            //当前用户
            var customer = Session["user"] as Customer;

            //当前用户的购物车
            var cart = bse.Carts.Where(c => c.customerId == customer.id).FirstOrDefault();

            //为当前用户创建一个订单
            Models.Order order = new Models.Order()
            {
                num = cart.num,
                price = cart.price,
                customer = cart.customerId,
                ordernum = Guid.NewGuid().ToString(),
                status = 0,
                receiverAddress = customer.address,
                receiverName = customer.username,
                receiverPhone = customer.telephone
            };
            order = bse.Orders.Add(order);

            //为订单添加明细
            foreach (var item in cart.CartItems)
            {
                var orderItem = new Models.OrdersItem()
                {
                    num = item.num,
                    price =  item.price,
                    bookId = item.bookId,
                    ordersId = order.id
                };
                order.OrdersItems.Add(orderItem);
            }

            //清空购物车
            bse.Carts.Remove(cart);

            bse.SaveChanges();

            return Redirect("~/Home/OrderInfo?orderId=" +order.id);
        }