加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – Mvc – 在会话中存储列表,然后检索其值

发布时间:2020-12-16 03:53:27 所属栏目:asp.Net 来源:网络整理
导读:您好,我正在尝试制作一个简单的在线杂志,并且当用户点击addtoCart按钮时,我得到了这个部分 我的模型购物车拥有两个属性 – 产品和数量 public class Cart{ public ProductLanguages Product { get; set; } public int Quantity { get; set; }} 所以在我的Add
您好,我正在尝试制作一个简单的在线杂志,并且当用户点击addtoCart按钮时,我得到了这个部分

我的模型购物车拥有两个属性 – 产品和数量

public class Cart
{

 public ProductLanguages Product { get; set; }
        public int Quantity { get; set; }

}

所以在我的AddProductToCart方法中的basketViewModel(类)中,我添加了产品,我从List的属性中获取了数据库的详细信息.

所以我无法弄清楚这个问题:控件中的某个地方我应该在Session中保存这个列表,如果用户下次我应该从这个会话中获取更多的产品.如果有人能给我一个带有索引动作的控制器的例子,我会非常感激.

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);

        }

解决方法

商店:

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

检索:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读