This project is read-only.

Fix the Cart Bug

Dec 30, 2010 at 5:13 PM

This bug occured when you add 2 or more same album to the cart.

when you remove one, the entry item will be deleted, it should decrease the quantity only.


here is the way to fix this bug that I used.

add a property in ShoppingCartRemoveViewModel

public int TheAlbumCount { get; set; }


get the count and return it to client by json in ShoppingCartController.cs

        public ActionResult RemoveFromCart(int id)
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);
            // Get the name of the album to display confirmation
            string albumName = storeDB.Carts
                .Single(item => item.RecordId == id).Album.Title;
            int albumCount = storeDB.Carts
                .Single(item => item.RecordId == id).Count;
            // Remove from cart. Note that for simplicity, we're 
            // removing all rather than decrementing the count.
            // Display the confirmation message
            var results = new ShoppingCartRemoveViewModel
                Message = Server.HtmlEncode(albumName) +
                    " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                DeleteId = id,
                TheAlbumCount = --albumCount
            return Json(results);

finally, update the view:

            // replace this $('#row-' + data.DeleteId).fadeOut('slow'); as follow
            if (data.TheAlbumCount == 0) {
                $('#row-' + data.DeleteId).fadeOut('slow');
            } else {
                $('#albumcount-' + data.DeleteId).text(data.TheAlbum);
                <!--Patch!--><div id="albumcount-<%: item.RecordId %>"><%: item.Count %></div>
it seems works fine now.
Jan 22, 2011 at 11:50 AM

It doesn't work as the grid does not get updated!

Could someone help