存储项目在会话变量一篮子 [英] Storing items for a basket in session variables
问题描述
我要存储项目产品codeS与该用户的会话阵列内选择的数量沿着购物篮。
我不知道最简单,最适当的数据类型与当前数据存储在什么。
我得到的,我不能只是保存我自己的对象会话中的IM pression?
产品code,数量
CQAR / HINCACOC,10
畜牧业协会/ VINNYMIN,20
块引用>我希望能够再次拉出来的值,以使它们被放置在篮子里。
我试过
列表<字典<字符串,整数>>
和的ArrayList
但既不似乎工作打算解决方案可以,只要该对象(和它有任何对象依赖性)被标记为
[序列化]
。在这种情况下,你会:[Serializable接口]
公共类BasketItem
{
公共字符串产品code {搞定;组; }
公众诠释数量{搞定;组; }
}然后,只需让这些对象的集合在你的会话存储,如
列表与LT; BasketItem>
更新:想我会根据下面的评论澄清,因为它是正确的,
[Serializable接口]
是的不如果会话被存储在进程/存储器所需。但是,如果该对象被存储在一个状态服务器/ SQL数据库因为对象必须序列/现场和会话存储之间反序列化,它是绝对必需的。I want to store item product codes for a shopping basket along with the quantity that the user has chosen within a session array.
I don't know what the easiest and most appropriate data type is to store this data in.
I get the impression that I can't just store my own object in a session?
Product code, Quantity CQAR/HINCACOC, 10 CAAA/VINNYMIN, 20
I wish to be able to pull the values out again in order that they were placed in the basket.
I tried
List<Dictionary<string, int>>
andArrayList
but neither seemed to work as intended.解决方案You can store your own custom list of objects in a session as long as that object (and any object dependencies it has) are marked as
[Serializable]
. In this case you would have:[Serializable] public class BasketItem { public string ProductCode { get; set; } public int Quantity { get; set; } }
Then just make a collection of these objects to store in your session, such as a
List<BasketItem>
Update: thought I would clarify based on the comment below as it is correct,
[Serializable]
is not required if the session is stored in-process/memory. But it is definitely required if the object is being stored in a state server/SQL database since the object must be serialized/deserialized between the site and session store.这篇关于存储项目在会话变量一篮子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!