/// <summary> /// 添加到购物车-首页添加 /// </summary> /// <param name="cvm"></param> /// <returns></returns> public ActionResult AddWithDefaultProduct(int productId) { try { using (LsBuyEntities db = new LsBuyEntities()) { string defaultColor = "red", defaultImage = "img1", defaultText = ""; //获取当前登录用户信息 User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); CustomerProduct cutomerPro = db.CustomerProducts.Where(t => t.Color == defaultColor && t.Text == defaultText && t.Image == defaultImage && t.ProductId == productId).FirstOrDefault(); if (cutomerPro == null) { CustomerProduct customerPro = new CustomerProduct { Color = defaultColor, Image = defaultImage, Text = defaultText, ProductId = productId, }; //添加定制产品 db.CustomerProducts.Add(customerPro); db.SaveChanges(); //查询定制产品 cutomerPro = db.CustomerProducts.Where(t => t.Color == defaultColor && t.Text == defaultText && t.Image == defaultImage && t.ProductId == 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; } }
public ActionResult Create([Bind(Include = "Id,UserName,Password,Email,UserType,Phone,Sex")] User user) { if (ModelState.IsValid) { //默认密码 123456 user.Password = "******"; db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public ActionResult Create([Bind(Include = "Id,ProductName,Price,UserId,CreatedTime,UpdatedTime,ModelNumber,Description,ImageUrl")] Product product) { if (ModelState.IsValid) { product.ImageUrl = SaveFile(); product.CreatedTime = DateTime.Now; product.UpdatedTime = DateTime.Now; product.UserId = User.Identity.Name; db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
/// <summary> /// 注册页面 /// </summary> /// <returns></returns> public ActionResult Register(RegistVModel registVModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { if (db.Users.Where(t => t.Email == registVModel.Email).Count() > 0) { ModelState.AddModelError("Email", "邮箱已注册!"); return(View("Regist", registVModel)); } else { Db.User user = new Db.User { Email = registVModel.Email, Phone = registVModel.Phone, UserType = 0, Password = registVModel.Password, UserName = registVModel.Email, Sex = registVModel.Sex }; db.Users.Add(user); db.SaveChanges(); } return(Redirect("Index")); } } else { return(View("Regist", registVModel)); } }
/// <summary> /// 添加收货地址 /// </summary> /// <param name="vModel"></param> /// <returns></returns> public ActionResult ConfirmAddAddress(AddressVModel vModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); Address address = new Address() { Content = vModel.Content, Phone = vModel.Phone, Receiver = vModel.Reveiver, UserId = user.Id, CreatedTime = DateTime.Now, UpdatedTime = DateTime.Now }; db.Addresses.Add(address); db.SaveChanges(); } return(Redirect("Addresses")); } else { return(View("AddAddress", vModel)); } }
/// <summary> /// 删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Remove(int id) { using (LsBuyEntities db = new LsBuyEntities()) { var shopchart = db.ShoppingCharts.Where(t => t.Id == id).FirstOrDefault(); db.ShoppingCharts.Remove(shopchart); db.SaveChanges(); return(RedirectToAction("ShoppingCharts", "PersonalCenter")); } }
/// <summary> /// 删除收货地址 /// </summary> /// <param name="id">收货地址Id</param> /// <returns></returns> public ActionResult DeleteAddress(int id) { using (LsBuyEntities db = new LsBuyEntities()) { var address = db.Addresses.Where(t => t.Id == id).FirstOrDefault(); db.Addresses.Remove(address); db.SaveChanges(); } return(Redirect("Addresses")); }
/// <summary> /// 提交订单 /// </summary> /// <returns></returns> public ActionResult ConfirmOrder(int[] id, string AddressId) { if (id == null || id.Length < 1 || string.IsNullOrEmpty(AddressId)) { using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); List <ShoppingChartVModel> spcharts = db.Database.SqlQuery <ShoppingChartVModel>(@"select t1.Id,t1.ProductId CustomerProductId,t2.Image, t2.Color,t2.Text,t2.ProductId, t3.Price,t3.ProductName,t3.ModelNumber,t3.ImageUrl from ShoppingCharts t1 left join CustomerProducts t2 on t1.ProductId= t2.Id left join Products t3 on t2.ProductId = t3.Id where t1.UserId = @userId", new SqlParameter("userId", user.Id)).ToList(); List <AddressVModel> addresses = db.Database.SqlQuery <AddressVModel>(@"select * from Addresses where UserId=@userId", new SqlParameter("userId", user.Id)).ToList(); ModelState.AddModelError("ValidMessage", "请选择收货地址或商品!"); return(View("ShoppingCharts", new ShoppingChartsVModel { Addresses = addresses, ShoppingCharts = spcharts })); } } using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); #region 生成订单 string orderNo = DateTime.Now.ToString("yyyyMMddHHmmss") + user.Id.ToString().PadLeft(5, '0'); Order order = new Order { AddressId = AddressId, OrderStatus = 0, UserId = user.Id, CreatedTime = DateTime.Now, OrderNo = orderNo }; db.Orders.Add(order); //订单详情 db.ShoppingCharts.Where(t => id.Contains(t.Id)).ToList() .ForEach(t => { OrderDetail detail = new OrderDetail { OrderNo = orderNo, ProductId = t.ProductId.ToString() }; db.OrderDetails.Add(detail); }); //保存 db.SaveChanges(); #endregion return(RedirectToAction("ConfirmOrderSuccess")); } }
/// <summary> /// 添加收货地址 /// </summary> /// <param name="vModel"></param> /// <returns></returns> public ActionResult ConfirmModifyAddress(AddressVModel vModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { Address address = db.Addresses.Where(t => t.Id == vModel.Id).FirstOrDefault(); address.Content = vModel.Content; address.Phone = vModel.Phone; address.Receiver = vModel.Reveiver; address.UpdatedTime = DateTime.Now; db.SaveChanges(); } return(Redirect("Addresses")); } else { return(View("ModifyAddress", vModel)); } }