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.