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); } }
private void detach_Orders(Order entity) { this.SendPropertyChanging(); entity.User = null; }
private void attach_Orders(Order entity) { this.SendPropertyChanging(); entity.User = this; }
partial void DeleteOrder(Order instance);
partial void UpdateOrder(Order instance);
partial void InsertOrder(Order instance);