public ActionResult Create(CartModels cartmodels) { if (ModelState.IsValid) { cartmodels.CartId = Guid.NewGuid().ToString(); db.CartModels.Add(cartmodels); db.SaveChanges(); return RedirectToAction("Index"); } return View(cartmodels); }
//支付确认首页 public ActionResult Index(string id) { //从字符串id中提取商品id和优惠码 string[] idArr = id.Split('|'); //得到优惠码 string coupon = idArr.Length > 1 ? idArr[1] : ""; //得到商品id id = idArr[0]; CartGuiModel cgm = new CartGuiModel(); //有优惠码的情况 if (string.IsNullOrEmpty(coupon)) { CartModels cm = db.CartModels.Find(id); if (cm != null) { List<CartModels> cartModelList = db.CartModels.ToList(); //组成能给用户看的购物车数据 cgm.Count = cartModelList.FindAll(e => e.ProductId == cm.ProductId).Count; cgm.Cart = cm; cgm.Product = db.ProductModels.Find(cm.ProductId); cgm.Profile = udb.UserProfiles.ToList().Find(u => u.UserName == cm.UserId); } } //没有优惠码的情况 else { //组成能给用户看的购物车数据 //加入用户数据 UserProfile up = new UserProfile(); up.UserId = new Random().Next(999); up.UserName = "******"; //加入数据库原始购物车数据 CartModels cm = new CartModels(); cm.CartId = Guid.NewGuid().ToString(); cm.ProductId = id; cm.UserId = up.UserId.ToString(); //加入商品数据 ProductModels pm = db.ProductModels.Find(id); pm.ProductPrice = (float)(pm.ProductPrice * 0.9); pm.ProductName += " (with 10% discount)"; cgm.Cart = cm; cgm.Product = pm; cgm.Profile = up; cgm.Count = 1; } return View(cgm); }
//将商品加入购物车,id为唯一标识商品的id public ActionResult AddToCart(string id = null) { //在数据库中找到该商品 ProductModels productmodels = db.ProductModels.Find(id); //创建一个购物车 CartModels cartmodels = new CartModels(); //为购物车指定是那个商品、那个用户买的、购物车本身的id cartmodels.CartId = Guid.NewGuid().ToString(); cartmodels.ProductId = id; cartmodels.UserId = User.Identity.Name; //存入数据库 db.CartModels.Add(cartmodels); db.SaveChanges(); //弹窗加入购物车成功 return Content("<script>alert('The item is added to your cart!');window.history.back();</script>"); }
public ActionResult Edit(CartModels cartmodels) { if (ModelState.IsValid) { db.Entry(cartmodels).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(cartmodels); }