Fix the Cart Bug

Dec 30, 2010 at 4: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; }

then 

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

        [HttpPost]
        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;
            //Patch!
            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.
            cart.RemoveFromCart(id);
            // 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,
                //Patch!
                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);
            }
... 
           <td>
                <!--Patch!--><div id="albumcount-<%: item.RecordId %>"><%: item.Count %></div>
            </td>
it seems works fine now.
Jan 22, 2011 at 10:50 AM

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

Could someone help