Tania Marinova
Tania Marinova

Reputation: 1898

Why my session in the view control is null

Hello I'm trying to make a simple online magazine and I get 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 in my AddProductToCart method, I add the product with details I get from database in property of type List. Then I save this list in a session. And if, for example, the user clicks 'continue to shop' button and returns to index page - next time when he presses 'addtocart' for the new product as you see I make this check

lstCarts = (List<Cart>)HttpContext.Current.Session["Cart"];
        if (lstCarts==null)
        {
           lstCarts = new List<Cart>();
        }

First, I try to get the list from session so i can get previously saved products. But my session is always null so i always lose my previously purchased products. The curious thing is that I'm sure that I saved the list of products in this line

        HttpContext.Current.Session["Cart"] = lstCarts;

Here is my viewmodel

 public class BasketViewModel
    {
    private readonly IProductLanguagesRepository prodlanRepository;
    public List<Cart> lstCarts { get; set; }
    public BasketViewModel() : this(new ProductLanguagesRepository())
    {

    }

    public BasketViewModel(IProductLanguagesRepository prodlanRepository)
    {
        this.prodlanRepository = prodlanRepository;
    }
    public void CreateCart(int id,int quantity)
    {
        lstCarts = (List<Cart>)HttpContext.Current.Session["Cart"];
        if (lstCarts==null)
        {
           lstCarts = new List<Cart>();
        }
        ProductLanguages nwProduct = prodlanRepository.GetProductDetails(id);
        if (nwProduct != null)
        {
            Cart cr = new Cart();
            cr.Product = nwProduct;
            cr.Quantity = quantity;
            lstCarts.Add(cr);
            HttpContext.Current.Session["Cart"] = lstCarts;
        }
    }      
}

And my Controller

 public class BasketManagementController : Controller
    {
    public ActionResult Index(int id, int quantity)
            {
                BasketViewModel vm = new BasketViewModel();
                vm.CreateCart(id, quantity);
                return View(vm);
            }
     }

Upvotes: 2

Views: 1668

Answers (2)

Uendel
Uendel

Reputation: 86

You are using the concepts wrong:

*The view model is for get and set properties object

*The controller is for operations with the view model objects

Please try like this bellow

//VIEW MODEL

   public class BasketViewModel{
    public List<Cart> lstCarts { get; set; }    
   }

//CONTROLLER


         public class BasketManagementController : Controller
        {

        private IProductLanguagesRepository prodlanRepository;

        public BasketManagementController(IProductLanguagesRepository prodlanRepository){

            this.prodlanRepository = prodlanRepository;

        } 

            public ActionResult Index(int id, int quantity)
             {
                CreateCart(id, quantity);
                BasketViewModel vm = new BasketViewModel();
                return View(vm);
             }

             public void CreateCart(int id,int quantity)
            {
                lstCarts = (List<Cart>)HttpContext.Current.Session["Cart"];
                if (lstCarts==null)
                {
                   lstCarts = new List<Cart>();
                }
                ProductLanguages nwProduct = prodlanRepository.GetProductDetails(id);
                if (nwProduct != null)
                {
                    Cart cr = new Cart();
                    cr.Product = nwProduct;
                    cr.Quantity = quantity;
                    lstCarts.Add(cr);
                    HttpContext.Current.Session["Cart"] = lstCarts;
                }
            } 


             }

Upvotes: 1

OyeHarish
OyeHarish

Reputation: 320

I have not seen certain behavior. May be you problem with the line

ProductLanguages nwProduct = prodlanRepository.GetProductDetails(id);

There may some exception with GetProductDetails(id)

Check out that.

Upvotes: 0

Related Questions