How to access values inside Hashtable list/Viewmod

2020-05-03 09:41发布

i have values inside a hashtable list like this enter image description here

i need the values inside of this.I dont know how .

view

 @foreach (var e in ViewData.Model )
        {
            //code which i need
        }

Controller

   public ActionResult test()
            {
                List<cart> cart=new List<Models.cart>();
                Hashtable ht = (Hashtable)Session["cart"];


                return View(ht);
}

Thanks in Advance

2条回答
再贱就再见
2楼-- · 2020-05-03 10:15

You are trying to access session from cshtml so you could do directly access:

(Hashtable)HttpContext.Current.Session["cart"]; 

or as you are setting Model by View(ht);

you can have by Model:

@Hashtable Model
..
@foreach (var e in Model )
{
}
查看更多
Anthone
3楼-- · 2020-05-03 10:24

I have done this in a different way

 public ActionResult AddToCarts(int mid,string mrate,string mname,string img,int mcount)
        {

                i++;
                Session["mid" + i] = mid;
                Session["mrate" + i] = mrate;
                Session["mname" + i] = mname;
                Session["mimage" + i] = img;
                Session["mcount" + i] = mcount;
                Session["count"] = i;
            return RedirectToAction("cart");
        }



public ActionResult cart()
        {
            List<cart> cart = new List<Models.cart>();
            for (int i = 1; i <= Convert.ToInt32(Session["count"]); i++)
            {
                cart c = new cart();
                c.mid = Convert.ToInt32(Session["mid" + i]);
                c.mprize = Convert.ToString(Session["mrate" + i]);
                c.img = Convert.ToString(Session["mimage" + i]);
                c.mname = Convert.ToString(Session["mname" + i]);
                c.mcount= Convert.ToInt32(Session["mcount" + i]);
                cart.Add(c);
            }

           return View("cart",cart);//,cart);

        }

and it works well

查看更多
登录 后发表回答