0

When I debugg my actionresult AddToCart. My cart gets a value of a productId which is working perfectly here it is:

public ActionResult AddToCart(int productID)
    {
        List<int> cart = (List<int>)Session["cart"];
        if (cart == null){
           cart = new List<int>();
        }
        cart.Add(productID);

        return new JsonResult() { Data = new { Status = "Success" } };
    }

And I am using ajax to get the productID:

$(function () {
            $(".a").live("click", function () {
                var $this = $(this),
            productID = $this.data('productID');
                $.ajax({
                    url: '/Home/AddToCart',
                    type: "post",
                    cache: false,
                    data: { productID: productID },
                    success: function (data) {
                        data = eval(data);
                        $this.addClass('productSelected');
                    },
                    error: function (result) {
                        $(".validation-summary-errors").append("<li>Error connecting to server.</li>");
                    }

                });
            });

        });

But when I implemented this actionresult my cart is null:

public ActionResult ShoppingCart()
{
    var cart = Session["cart"] as List<int>;

    var products = cart != null ? cart.Select(id => 
             {
                 var product = repository.GetProductById(id);
                 return new ProductsViewModel
                    {
                       Name = product.Name,
                       Description = product.Description,
                       price = product.Price
                    }
             }) : new List<ProductsViewModel>();

    return PartialView(products);
}

What am I doing wrong? Why is actionresult Shoppingcart have null values how is this happenin? When I debugg AddToCart, Cart gets a value and it gets added.

Obsivus
  • 8,231
  • 13
  • 52
  • 97

2 Answers2

2

you should update the cart object inside the session after adding an item to it:

public ActionResult AddToCart(int productID)
{
    List<int> cart = (List<int>)Session["cart"];
    if (cart == null){
       cart = new List<int>();
    }
    cart.Add(productID);

    Session["cart"] = cart;

    return new JsonResult() { Data = new { Status = "Success" } };
}
tucaz
  • 6,524
  • 6
  • 37
  • 60
  • IIRC this is only necessary in the session-variable-was-null case; if there was already a cart in the session then the session variable and the local variable refer to the same list, and updating one updates the other. (See e.g. [here](http://stackoverflow.com/questions/6380842/asp-net-do-changes-to-session-objects-persist).) – Rawling Oct 31 '12 at 11:09
1

You need to update the session cart too, like this...

public ActionResult AddToCart(int productID)
{
    List<int> cart = (List<int>)Session["cart"];
    if (cart == null){
       Session["cart"] = cart = new List<int>();
    }
    cart.Add(productID);

    return new JsonResult() { Data = new { Status = "Success" } };
}
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30