/// <summary> /// 添加到购物车 /// </summary> /// <param name="cvm"></param> /// <returns></returns> public ActionResult Add(CustomerMadeVModel cvm) { try { using (LsBuyEntities db = new LsBuyEntities()) { //获取当前登录用户信息 User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); CustomerProduct cutomerPro = db.CustomerProducts.Where(t => t.Color == cvm.CustomerProduct.Color && t.Text == cvm.CustomerProduct.Text && t.Image == cvm.CustomerProduct.Image && t.ProductId == cvm.CustomerProduct.ProductId).FirstOrDefault(); if (cutomerPro == null) { CustomerProduct customerPro = new CustomerProduct { Color = cvm.CustomerProduct.Color, Image = cvm.CustomerProduct.Image, Text = cvm.CustomerProduct.Text, ProductId = cvm.CustomerProduct.ProductId, }; //添加定制产品 db.CustomerProducts.Add(customerPro); db.SaveChanges(); //查询定制产品 cutomerPro = db.CustomerProducts.Where(t => t.Color == cvm.CustomerProduct.Color && t.Text == cvm.CustomerProduct.Text && t.Image == cvm.CustomerProduct.Image && t.ProductId == cvm.CustomerProduct.ProductId).FirstOrDefault(); } //加入购物车 ShoppingChart shoppingChart = new ShoppingChart { ProductId = cutomerPro.Id, UserId = user.Id, CreatedTime = DateTime.Now }; //加入购物车 db.ShoppingCharts.Add(shoppingChart); //保存到数据库 db.SaveChanges(); return(Redirect("AddSuccess")); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 定制页面主页 /// </summary> /// <returns></returns> public ActionResult Index(int pid) { using (LsBuyEntities db = new LsBuyEntities()) { CustomerMadeVModel vm = new CustomerMadeVModel(); Product product = db.Products.Where(t => t.Id == pid).FirstOrDefault(); vm.Product = new ProductVModel { Id = product.Id, Price = product.Price, ImageUrl = product.ImageUrl, Description = product.Description, ModelNumber = product.ModelNumber, ProductName = product.ProductName }; vm.CustomerProduct = new CustomerProductVModel { ProductId = product.Id, Color = "red", Image = "img1", Text = "", }; return(View(vm)); } }