Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello can any one help me with links or code for mycart.. My problem is that when i click a object in products page its going into my cart page.. but how many times im click add to cart button.. that many times its been submitting even thought its the same item. i want to change it into like.. when i click on add to cart button if that item is already exists in the mycart it has to get added to existing item(if both are same). if not then add to cart as 2nd item. please its an urgent assignment plz do help me....
Posted
Comments
Vamna 4-Jun-13 0:55am    
please do tell me wrt MVC 4.0.. not by asp.net
jeyamothi 5-Jun-13 8:29am    
when u add the item to cart at that time u change the status from "Avail" to "Reserved". it avoids to add duplicates

You can make it by something like this:
C#
public void AddItem(int productId) {
        // Create a new item to add to the cart
        CartItem newItem = new CartItem(productId);

        // If this item already exists in our list of items, increase the quantity
        // Otherwise, add the new item to the list
        if (Items.Contains(newItem)) {
            foreach (CartItem item in Items) {
                if (item.Equals(newItem)) {
                    item.Quantity++;
                    return;
                }
            }
        } else {
            newItem.Quantity = 1;
            Items.Add(newItem);
        }
    }

Refer article Build a Shopping Cart in ASP.NET[^]

Another way:
e.g:
C#
public void AddToCart(Album album)
        {
            // Get the matching cart and album instances
            var cartItem = storeDB.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                && c.AlbumId == album.AlbumId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    AlbumId = album.AlbumId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                storeDB.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart,
                // then add one to the quantity
                cartItem.Count++;
            }
            // Save changes
            storeDB.SaveChanges();
        }

Found it: here[^]
 
Share this answer
 
v2
Comments
Vamna 3-Jun-13 7:21am    
is there any other solution apart from the above plzz do keep...
Create a private method like this.
C#
private List<MYCart> sessionOfCarts
{
    get
    {
        if (Session["sessionOfCarts"] == null)
            this.sessionOfCarts = new List<MYCart>();
        return (List<MYCart>)Session["sessionOfCarts"];
    }
    set
    {
        Session["sessionOfCarts"] = value;
    }
}


//Main Method...

C#
public void AddMyCart(MYCart selectedItem)
 {
     if(sessionOfCarts.Any(p =>p.cartId == selectedItem.cartId))
     {
         var sample = sessionOfCarts.Select(p =>{
                  if (p.cartId == selectedItem.cartId)
                  { p.Quantity = p.Quantity + 1; }; return p; }).ToList();
     }
     else
     {
       sessionOfCarts.Add(selectedItem);
     }

 }



You can get the selected carts from sessionOfCarts..

Clear that Session once you complete this process..
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900