2
votes

These are the two actionresults I use for my cart:

The first action I am adding the productids to my session cart by using ajax:

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

    Session["cart"] = cart;

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

The second action I am filling it to my model so I can use it in my view:

public ActionResult ShoppingCart()
        {
        var cart = Session["cart"] as List<int>;
        var products = cart != null ? cart.Select(id => 
             {
                 var productImage = repository.GetProductByID(id);
                 return new ProductsViewModel
                    {
                        Name = productImage.Products.Name,
                        Description = productImage.Products.Description.Substring(0, productImage.Products.Description.IndexOf(".") + 1),
                        price = productImage.Products.Price,
                        ProductId = productImage.Products.Id,
                        Image = productImage.ImageUrl,
                        ProductImageId = productImage.Id
                    };
             }) : new List<ProductsViewModel>();

      return View(products);
        }

Right now I have a view that is displaying the products in the shoppingcart view.

Each product that is displayed have a button that says "Delete"

My question is how can I delete the product from the session cart in the best way?

How should the Actionresult look like?

I have created an Ajax script to gets the ID of the product that needs to be deleted when someone click on the delete button here it is:

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

                });
            });

        });

If I am thinkin right with the javascript, how should the actionresult look like if I want delete the product by the ID I recieved?

I guess It should look something like this:

public ActionResult DeleteProductFromCart(int ProductImageId)
{
 // code..
}

If I am still thinkin right how should I code inside the brackets on DeleteProductFromCart?

Any kind of help is appreciated.

2

2 Answers

2
votes

I think it should be very similar to the add. I added an extra check since it's not logical to try to remove something from a cart that dosn't exist

public ActionResult DeleteProductFromCart(int ProductImageId)
{
    List<int> cart = (List<int>)Session["cart"];
    if (cart == null)
    {
        return new JsonResult() { Data = new { Status = "ERROR" } };
    }
    cart.Remove(ProductImageId);

    return new JsonResult() { Data = new { Status = "Success" } };
}
1
votes
public ActionResult DeleteProductFromCart(int ProductImageId)
{
    var cart = Session["cart"] as List<int>;
    cart.Remove(ProductImageId);

    ... return result
}