Mvc - store a list in session and then retrieve its value

asp.net-mvc

Solution

Store:

HttpContext.Session["list"] = new List<object> { new object(), new object() };

Retrieve:

var list = HttpContext.Current.Session["list"] as List<object>;

Problem

Hello I'm trying to make a simple online magazine and I got to the part where when the user clicks addtoCart button My model Cart holds two properties - product and Quantity ``` public class Cart { public ProductLanguages Product { get; set; } public int Quantity { get; set; } } ``` So in my basketViewModel (class) inside my AddProductToCart method I add the product, the details for which I get from database in property of List. So I can't figure out this issue: Somewhere in the control I should save this list in a Session and if the user adds more products the next time I should get the list from this session. If someone can give me an example of controler with an index action that can do this I would be really thankful. ``` public class BasketViewModel { private readonly IProductLanguagesRepository prodlanRepository; public List<Cart> listProductsinBasket { get; set; } public BasketViewModel() : this(new ProductLanguagesRepository()) { } public BasketViewModel(IProductLanguagesRepository prodlanRepository) { this.prodlanRepository = prodlanRepository; } public void AddProductToCart(int id,int quantity) { ProductLanguages nwProduct = prodlanRepository.GetProductDetails(id); if (nwProduct != null) { Cart cr = new Cart(); cr.Product = nwProduct; cr.Quantity = quantity; listProductsinBasket.Add(cr); } ```

Original source