Ejemplo n.º 1
0
        //bæti við í körfu
        public bool AddToCart(int id, string userId, int cartId)
        {
            var itemincart = (from it in _db.BooksInCarts
                              where it.UserId == userId && id == it.BookId && cartId == it.OrderId
                              select new BooksInCart
            {
                Id = it.Id,
                BookId = id,
                Quantity = it.Quantity,
                OrderId = it.Id,
                UserId = it.UserId,
            }).FirstOrDefault();

            if (itemincart == null)
            {
                //býr til nýjan hlut  ef bókin er ekki í körfu
                itemincart = new BooksInCart
                {
                    BookId   = id,
                    Quantity = 1,
                    OrderId  = cartId,
                    UserId   = userId,
                };
                _db.BooksInCarts.Add(itemincart);
            }
            else
            {
                //ef bókin er þegar til
                itemincart.Quantity++;
                _db.BooksInCarts.Update(itemincart);
            }
            //cartId.TotalPrice += itemincart.Price;
            _db.SaveChanges();
            return(true);
        }
Ejemplo n.º 2
0
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                int RowClicked = Convert.ToInt32(e.CommandArgument);
                int bookid     = Convert.ToInt32(GridView1.DataKeys[RowClicked].Value);

                List <int> BooksInCart = (List <int>)Session["Cart"];

                if (BooksInCart == null)
                {
                    BooksInCart = new List <int>();
                }
                BooksInCart.Add(bookid);
                Session["Cart"] = BooksInCart;
            }
        }
Ejemplo n.º 3
0
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //the  e. command name calls the function of the button and store it in the cart session in the list array
            if (e.CommandName == "AddToCart")
            {
                int RowClicked = Convert.ToInt32(e.CommandArgument);
                int bookid     = Convert.ToInt32(GridView1.DataKeys[RowClicked].Value);

                //list array to store the values of the selected column into the session array

                List <int> BooksInCart = (List <int>)Session["Cart"];

                if (BooksInCart == null)
                {
                    BooksInCart = new List <int>();
                }
                BooksInCart.Add(bookid);
                Session["Cart"] = BooksInCart;
            }
        }