コード例 #1
0
        public ActionResult SubmitOrder(int userid, string bookids, string qtys)
        {
            try
            {
                var items = new List<OrderItemParm>();
                var bookidArray = bookids.Split(',').Select(a=> int.Parse(a)).ToList();
                var qtyArray = qtys.Split(',').Select(a => int.Parse(a)).ToList();
                for (int i = 0; i < bookidArray.Count; i++)
                {
                    items.Add(new OrderItemParm
                    {
                        bookid = bookidArray[i],
                        qty = qtyArray[i]
                    });
                }
                using (BookStoreDataContext data = new BookStoreDataContext())
                {
                    var user = data.Users.SingleOrDefault(_ => _.ID == userid);
                    if (user != null)
                    {
                        var order = new Order
                        {
                            Time = DateTime.Now,
                            UserId = userid,
                        };
                        foreach (var bookid in bookidArray)
                        {
                            var book = data.Books.SingleOrDefault(b => b.ID == bookid);
                            if (book != null)
                            {
                                var oi = new OrderItem
                                {
                                    BookId = bookid,
                                    Price = book.Price,
                                    Qty = items.Single(a => a.bookid == bookid).qty,
                                };
                                oi.TotalPrice = oi.Price * oi.Qty;
                                order.OrderItems.Add(oi);
                            }
                        }

                        order.TotalPrice = order.OrderItems.Sum(_ => _.TotalPrice);
                        data.Orders.InsertOnSubmit(order);
                        data.SubmitChanges();
                        return Json(new { results = new { orderid = order.ID }, success = true }, JsonRequestBehavior.AllowGet);
                    }
                    else
                        return Json(new { success = false, reason = "usernotexists" }, JsonRequestBehavior.AllowGet);

                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, reason = "servererror" }, JsonRequestBehavior.AllowGet);
            }
        }
コード例 #2
0
		private void detach_Orders(Order entity)
		{
			this.SendPropertyChanging();
			entity.User = null;
		}
コード例 #3
0
		private void attach_Orders(Order entity)
		{
			this.SendPropertyChanging();
			entity.User = this;
		}
コード例 #4
0
 partial void DeleteOrder(Order instance);
コード例 #5
0
 partial void UpdateOrder(Order instance);
コード例 #6
0
 partial void InsertOrder(Order instance);