//新增一筆Product,使用ProductId public bool AddProduct(int ProductId) { var findItem = this.cartItems .Where(s => s.Id == ProductId) .Select(s => s) .FirstOrDefault(); //判斷相同Id的CartItem是否已經存在購物車內 if (findItem == default(CartItem)) { //不存在購物車內,則新增一筆 using (Models.CartsEntities db = new CartsEntities()) { var product = (from s in db.Products where s.Id == ProductId select s).FirstOrDefault(); if (product != default(Models.Product)) { this.AddProduct(product); } } } else { //存在購物車內,則將商品數量累加 findItem.Quantity += 1; } return(true); }
public ActionResult MyOrder() { //取得目前登入使用者Id var userId = HttpContext.User.Identity.GetUserId(); using (MVC_Carts.Models.CartsEntities db = new MVC_Carts.Models.CartsEntities()) { var result = (from s in db.Orders where s.UserId == userId select s).ToList(); return(View(result)); } }
public ActionResult Index(Models.OrderModel.Ship postback) { if (ModelState.IsValid) { //取得目前購物車 var currentcart = MVC_Carts.Models.Operation.GetCurrentCart(); //取得目前登入使用者Id string userId = HttpContext.User.Identity.GetUserId(); if (userId == null) { return(RedirectToAction("Login", "Account")); } using (MVC_Carts.Models.CartsEntities db = new MVC_Carts.Models.CartsEntities()) { //建立Order物件 var order = new MVC_Carts.Models.Order() { UserId = userId, ReceiverName = postback.RecieverName, ReceiverPhone = postback.RecieverPhone, ReceiverAddress = postback.RecieverAddress }; //加其入Orders資料表後,儲存變更 db.Orders.Add(order); db.SaveChanges(); //取得購物車中OrderDetai物件 var orderDetails = currentcart.ToOrderDetailList(order.Id); //更新商品庫存數量 for (int i = 0; i < orderDetails.Count; i++) { int productId = Convert.ToInt32(orderDetails[i].Id); var result = (from p in db.Products where p.Id == productId select p).FirstOrDefault(); result.Quantity = result.Quantity - orderDetails[i].Quantity; } //將其加入OrderDetails資料表後,儲存變更 db.OrderDetails.AddRange(orderDetails); db.SaveChanges(); //清空購物車商品 currentcart.ClearCart(); } //DB連線釋放 } return(RedirectToAction("Success")); }
public ActionResult MyOrderDetail(int id) { using (MVC_Carts.Models.CartsEntities db = new MVC_Carts.Models.CartsEntities()) { var result = (from s in db.OrderDetails where s.OrderId == id select s).ToList(); if (result.Count == 0) { return(RedirectToAction("Index")); } else { return(View(result)); } } }