//支付确认首页 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); }
// 购物车首页 // GET: /Cart/ public ActionResult Index() { //用于显示给用户的购物车数据链表 List<CartGuiModel> cartGuiModelList = new List<CartGuiModel>(); //直接从数据库取到的购物车数据链表 List<CartModels> cartModelList = db.CartModels.ToList(); //将数据库原始数据逐个转化成能给用户看的数据,包括谁是购买者(Profile),商品详细信息(Product),购买数量(Count) foreach (CartModels cm in cartModelList) { if (User.Identity.Name == cm.UserId && cartGuiModelList.Find(e => e.Product.ProductId == cm.ProductId) == null) { CartGuiModel cgm = new CartGuiModel(); //购买数量 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); //加入用于显示给用户的购物车数据链表 cartGuiModelList.Add(cgm); } } return View(cartGuiModelList); }