为什么我在视图控制会话为null [英] Why my session in the view control is null

查看:97
本文介绍了为什么我在视图控制会话为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想做一个简单的在线杂志,我到了那里的部分,当用户点击 addtoCart 按钮

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

 公共类车
{ 公共ProductLanguages​​产品{搞定;组; }
        公众诠释数量{搞定;组; }}

所以在我的 basketViewModel 在我的 AddProductToCart 的方法,我想补充的产品,详情我在属性数据库获得类型列表。然后我保存这个列表中的会话。
如果,例如,用户点击继续开店按钮,返回到索引页 - 当他pressesaddtocart的新产品,你看我这个做检查下一次

  lstCarts =(列表<购物车和GT;)HttpContext.Current.Session [车];
        如果(lstCarts == NULL)
        {
           lstCarts =新的List<购物车和GT;();
        }

首先,我试图从会话列表,以便我可以得到previously保存的产品。
但我会始终为空,所以我总是失去我的previously购买的产品。好奇的是,我敢肯定,我保存在这条线的产品清单

  HttpContext.Current.Session [购物车] = lstCarts;

下面是我的视图模型

 公共类BasketViewModel
    {
    私人只读IProductLanguages​​Repository prodlanRepository;
    公开名单<购物车和GT; lstCarts {搞定;组; }
    公共BasketViewModel():这个(新ProductLanguages​​Repository())
    {    }    公共BasketViewModel(IProductLanguages​​Repository prodlanRepository)
    {
        this.prodlanRepository = prodlanRepository;
    }
    公共无效CreateCart(INT ID,INT数量)
    {
        lstCarts =(列表<购物车和GT;)HttpContext.Current.Session [车];
        如果(lstCarts == NULL)
        {
           lstCarts =新的List<购物车和GT;();
        }
        ProductLanguages​​ nwProduct = prodlanRepository.GetProductDetails(ID);
        如果(nwProduct!= NULL)
        {
            车CR =新的购物车();
            cr.Product = nwProduct;
            cr.Quantity =数量;
            lstCarts.Add(CR);
            HttpContext.Current.Session [车] = lstCarts;
        }
    }
}

和我控制器

 公共类BasketManagementController:控制器
    {
    公众的ActionResult指数(INT ID,INT数量)
            {
                BasketViewModel VM =新BasketViewModel();
                vm.CreateCart(ID,数量);
                返回查看(VM);
            }
     }


解决方案

您正在使用的概念错了:

*视图模型是get和set属性对象

*控制器是与视图模型对象的操作

请尝试这样的波纹管

  //视图模型   公共类BasketViewModel {
    公开名单<购物车和GT; lstCarts {搞定;组; }
   }//控制器
         公共类BasketManagementController:控制器
        {        私人IProductLanguages​​Repository prodlanRepository;        公共BasketManagementController(IProductLanguages​​Repository prodlanRepository){            this.prodlanRepository = prodlanRepository;        }            公众的ActionResult指数(INT ID,INT数量)
             {
                CreateCart(ID,数量);
                BasketViewModel VM =新BasketViewModel();
                返回查看(VM);
             }             公共无效CreateCart(INT ID,INT数量)
            {
                lstCarts =(列表<购物车和GT;)HttpContext.Current.Session [车];
                如果(lstCarts == NULL)
                {
                   lstCarts =新的List<购物车和GT;();
                }
                ProductLanguages​​ nwProduct = prodlanRepository.GetProductDetails(ID);
                如果(nwProduct!= NULL)
                {
                    车CR =新的购物车();
                    cr.Product = nwProduct;
                    cr.Quantity =数量;
                    lstCarts.Add(CR);
                    HttpContext.Current.Session [车] = lstCarts;
                }
            }
             }

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

解决方案

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


             }

这篇关于为什么我在视图控制会话为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆