ASP.net,将对象添加到会话变量 [英] ASP.net, adding objects to a session variable

查看:103
本文介绍了ASP.net,将对象添加到会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在回传的对象(S)添加到会话变量。我的code类似于以下内容。

I'm trying to add an object(s) to a session variable across postbacks. My code looks similar to the following.

  Supply sup =  Supplies.GetSupply(supplyItemID);

  Session["CartObjects"] += sup;

现在,编译器会引发错误,指出了+ =操作符不能在类型'对象'和'供应'被使用。我需要实现我供应对象上允许添加它的接口?这是可能的还是我在完全错误的方式思考这个问题。

Now, the compiler throws an error saying that the "+=" operator cannot be used on type 'object' and 'Supply'. Do I need to implement an interface on my Supply object that allows it to be added? Is this possible or am I thinking about this in the completely wrong way.

推荐答案

试试这个:

Supply sup =  Supplies.GetSupply(supplyItemID); 
var cartObjects =  (Session["CartObjects"] as List<Supply>) ?? new List<Supply>();
cartObjects.Add(sup);
Session["CartObjects"] = cartObjects;

这篇关于ASP.net,将对象添加到会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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