public ActionResult Agregar(int id)
        {
            ProdCarro carro = new ProdCarro();

            if (Session["cart"] == null)
            {
                List <Item> cart = new List <Item>();
                Producto    p    = carro.find(id);
                string      nam  = p.nombre_producto;
                cart.Add(new Item {
                    Producto = carro.find(id), Cantidad = 1
                });
                Session["cart"] = cart;
            }
            else
            {
                List <Item> cart  = (List <Item>)Session["cart"];
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Cantidad++;
                }
                else
                {
                    Producto p   = carro.find(id);
                    string   nam = p.nombre_producto;
                    cart.Add(new Item {
                        Producto = carro.find(id), Cantidad = 1
                    });
                }
                Session["cart"] = cart;
            }
            return(RedirectToAction("Index"));
        }
Exemple #2
0
        public ActionResult Agregar(int id, int cantidad)
        {
            //Cantidad agregar otro parametro
            ProdCarro carro = new ProdCarro();

            if (Session["cart"] == null)
            {
                List <Item> cart = new List <Item>();
                Producto    p    = carro.find(id);
                string      nam  = p.nombre_producto;
                cart.Add(new Item {
                    Producto = carro.find(id), Cantidad = cantidad
                });
                Session["cart"]      = cart;
                Session["itemTotal"] = cantidad;
            }
            else
            {
                List <Item> cart  = (List <Item>)Session["cart"];
                int         index = isExist(id);
                if (index != -1)
                {
                    cart[index].Cantidad += cantidad;
                    Session["itemTotal"]  = ((int)Session["itemTotal"]) + cantidad;
                }
                else
                {
                    Producto p   = carro.find(id);
                    string   nam = p.nombre_producto;
                    cart.Add(new Item {
                        Producto = carro.find(id), Cantidad = cantidad
                    });
                    Session["itemTotal"] = ((int)Session["itemTotal"]) + cantidad;
                }
                Session["cart"] = cart;
            }
            return(RedirectToAction("Index"));
        }