asp.net mvc - Why my session in the view control is null -


hello i'm trying make simple online magazine , part when user clicks addtocart button

my model cart holds 2 properties - product , quantity

public class cart {   public productlanguages product { get; set; }         public int quantity { get; set; }  } 

so in basketviewmodel in addproducttocart method, add product details database in property of type list. save list in session. , if, example, user clicks 'continue shop' button , returns index page - next time when presses 'addtocart' new product see make check

lstcarts = (list<cart>)httpcontext.current.session["cart"];         if (lstcarts==null)         {            lstcarts = new list<cart>();         } 

first, try list session can saved products. session null lose purchased products. curious thing i'm sure saved list of products in line

        httpcontext.current.session["cart"] = lstcarts; 

here 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 controller

 public class basketmanagementcontroller : controller     {     public actionresult index(int id, int quantity)             {                 basketviewmodel vm = new basketviewmodel();                 vm.createcart(id, quantity);                 return view(vm);             }      } 

you using concepts wrong:

*the view model , set properties object

*the controller operations view model objects

please try 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;                 }             }                 } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -