//加入購物車 public JsonResult AddToCart(int ProductId, int buyQty) { if (Session[CSession關鍵字.SK_LOGINED_CUSTOMER] != null) { tCustomer cust = Session[CSession關鍵字.SK_LOGINED_CUSTOMER] as tCustomer; MotaiDataEntities db = new MotaiDataEntities(); var product = (new MotaiDataEntities()).tProducts.FirstOrDefault(p => p.ProductId == ProductId); if (product != null && product.pQty > buyQty) { tStatu cart = new tStatu(); cart.sCustomerId = cust.CustomerId; cart.sProductId = ProductId; cart.sProductQty = buyQty; db.tStatus.Add(cart); db.SaveChanges(); return(Json(new { result = true, msg = "加入成功" })); } else { return(Json(new { result = false, msg = "庫存不足" })); } } else { return(Json(new { result = false, msg = "請先登入" })); } }
public ActionResult 購物車內刪除(int fid) { MotaiDataEntities db = new MotaiDataEntities(); tStatu statu = db.tStatus.Where(s => s.StatusId.Equals(fid)).FirstOrDefault(); if (statu != null) { db.tStatus.Remove(statu); db.SaveChanges(); } return(RedirectToAction("購物車清單")); }
//加入購物車 public JsonResult AddToCart(int ProductId, int buyQty) { if (Session[CSession關鍵字.SK_LOGINED_CUSTOMER] != null) { MotaiDataEntities db = new MotaiDataEntities(); tCustomer cust = Session[CSession關鍵字.SK_LOGINED_CUSTOMER] as tCustomer; int count = db.tStatus.Where(c => c.sCustomerId == cust.CustomerId).ToList().Count; ViewBag.Count = count + "項"; tStatu productStatus = db.tStatus.Where(s => s.sCustomerId == cust.CustomerId && s.sProductId == ProductId).FirstOrDefault(); if (productStatus != null) { int statusQty = 0; statusQty = productStatus.sProductQty; var product = (new MotaiDataEntities()).tProducts.FirstOrDefault(p => p.ProductId == ProductId); int productQty = productRespotiory.GetProductQtyById(ProductId); if (product != null && productQty - statusQty >= buyQty) { productStatus.sProductQty += buyQty; db.SaveChanges(); return(Json(new { result = true, msg = "加入成功" })); } else { return(Json(new { result = false, msg = "庫存不足" })); } } else { var product = (new MotaiDataEntities()).tProducts.FirstOrDefault(p => p.ProductId == ProductId); int productQty = productRespotiory.GetProductQtyById(ProductId); if (product != null && productQty >= buyQty) { tStatu cart = new tStatu(); cart.sCustomerId = cust.CustomerId; cart.sProductId = ProductId; cart.sProductQty = buyQty; db.tStatus.Add(cart); db.SaveChanges(); return(Json(new { result = true, msg = "加入成功" })); } else { return(Json(new { result = false, msg = "庫存不足" })); } } } else { return(Json(new { result = false, msg = "請先登入" })); } }
public ActionResult 購物車內刪除(int fid) { MotaiDataEntities db = new MotaiDataEntities(); tStatu statu = db.tStatus.Where(s => s.StatusId.Equals(fid)).FirstOrDefault(); if (statu != null) { db.tStatus.Remove(statu); db.SaveChanges(); tCustomer cust = Session[CSession關鍵字.SK_LOGINED_CUSTOMER] as tCustomer; int count = db.tStatus.Where(c => c.sCustomerId == cust.CustomerId).ToList().Count; ViewBag.Count = count + "項"; } return(RedirectToAction("購物車清單")); }