Ejemplo n.º 1
0
        public async Task <ActionResult> DeleteFromCart(ShopCartVM model)
        {
            using (Db db = new Db())
            {
                ShopCartDB modelDb = await db.ShopCarts.FindAsync(model.Id);

                db.ShopCarts.Remove(modelDb);
                db.SaveChanges();

                ViewBag.MessageInfo = model.ProductName + " is delete";

                modelDb = await db.ShopCarts.FindAsync(model.Id);

                if (modelDb != null)
                {
                    ViewBag.MessageInfo = modelDb.ProductName + " is't delete";
                }
                else
                {
                    var listCart = (List <ShopCartVM>)Session["Cart"];

                    if (Session["Cart"] != null)
                    {
                        for (int i = 0; i < listCart.Count; i++)
                        {
                            if (listCart[i].Id == model.Id)
                            {
                                listCart.RemoveAt(i);
                                Session["Cart"] = listCart;
                                break;
                            }
                        }
                    }
                    //await Index(null);
                }
            }
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult CartPartial()
        {
            if (Session["UserId"] == null)
            {
                //var user = HttpContext.User.Identity.Name.ToString();
                UserModel user = UserManager.FindByEmail(HttpContext.User.Identity.Name);

                if (user == null)
                {
                    return(RedirectToAction("Logout", "Account"));
                }

                Session["UserId"] = user.Id;

                using (Db db = new Db())
                {
                    Session["Cart"] = db.ShopCarts.ToArray().Where(m => m.UserId == user.Id).Select(m => new ShopCartVM(m)).ToList();
                }
            }

            ShopCartVM model = new ShopCartVM();

            if (Session["Cart"] != null)
            {
                foreach (var product in (List <ShopCartVM>)Session["Cart"])
                {
                    model.AmountProduct += product.AmountProduct;
                    model.Price         += product.Total;
                }
            }
            else
            {
                model.AmountProduct = 0;
                model.Price         = 0m;
            }

            return(PartialView("_CartPartial", model));
        }