asp.net mvc - retrieve session value in view page -


i started learn how build website, , have problem using session create shopping cart.

here model code:

public class cart {     public cart()     {         products = new list<productstock>();     }      public list<productstock> products { get; set; }      public void addtocart(product product, store store, int quantity)     {         products.add(new productstock         {             productid = product.productid,             product = product,             storeid = store.storeid,             store = store,             quantity = quantity         });     } } 

here controller code, session want use in view page called "cart":

public class cartcontroller : controller {     private magicinventoryentities db = new magicinventoryentities();      public actionresult index()     {         cart cart = (cart) session["cart"];          return view(cart);     }      [httppost]     public actionresult addtocart(int productid, int storeid, int quantity)     {         product product = db.products.find(productid);         store store = db.stores.find(storeid);          cart cart = (cart) session["cart"];          cart.addtocart(product, store, quantity);          return new redirecttorouteresult(new routevaluedictionary(         new         {             action = "index",             controller = "cart"         }));     }      protected override void dispose(bool disposing)     {         if (disposing)         {             db.dispose();         }         base.dispose(disposing);     } } 

this code teacher, notes not clear , didn't have enough time write details view page. said show process again on monday, don't want wait, want figure out , move on. know basic method call session "@session["sessionname"].tostring() ". have tried in method( session["testing"]=productid ), , can value. however, don't how value session["cart"].

assuming have object of type cart saved in session, in order retrieve object can use following code in index view:

 @{     cart cart = httpcontext.current.session["cart"] cart;   } 

however, see index method in cartcontroller retrieves object session , passes index view. in order access have write following code in index view:

@model {projectname}.models.cart  //you tell view type of object should expect receive; instead of {projectname} write project name   @foreach(var product in model.products) {  //you access received object using keyword model     <div>         @product.productid     </div> } 

as can see, examples creates div each item of type productstock in products list, , writes value of current productstock productid inside of it.

if not have object of type cart saved in session, add following code index method:

if(session["cart"] == null) {     var cart = new cart();     session["cart"] = cart; }  cart cart = (cart) session["cart"]; return view(cart); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -