问题
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>>
and ArrayList
but neither seemed to work as intended.
回答1:
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.
回答2:
I get the impression that I can't just store my own object in a session
.
Why do you get that impression? You can store your own objects in session. IF you need a web farm and are storing session in a database then make sure to mark your objects with [Serializable]
.
To track order, you can add another property to your object. I would suggest something like this:
[Serializable]
public class CartEntry
{
public string ProductCode { get; set; }
public int Quantity { get; set; }
public int Order { get; set; }
}
回答3:
Create a List with BasketItems
public class CartItem
{
public int ProductID {get;set;}
public int Quantity {get;set;}
}
List<CartItem> items = new List<CartItem>();
items.Add(new CartItem() { ProductID = 1, Quantity = 2 })
Session["cartItems"] = items;
to get it back again
if(Session["cartItems"] != null)
List<CartItem> items = (List<CartItem>)Session["cartItems"];
To get an item by Product ID see below
int productId = 1;
var cartItem = (from ci in items where ci.ProductID == productId select ci).FirsOrDefault();
回答4:
Have you considered the SortedDictionary object? Find reference here http://msdn.microsoft.com/en-us/library/f7fta44c.aspx . As a small aside are you sure you want to do this using Session if you're using an InProc or ASP.NET Session Manager service and get a lot of visits you could run into out of memory issues. Consider moving this structure to the DB or possibly implementing Session using SQL Server. Check here for Session state modes info and how to configure for SQL Server state management http://msdn.microsoft.com/en-us/library/ms178586.aspx .
回答5:
there is another thread on stackoverflow that describes a solution for your requirement:
Saving List to a Session in ASP.NET
The whole process with examples is also described on MSDN:
http://msdn.microsoft.com/en-us/library/ms178581.aspx
I also created an example to show how to store and restore an ArrayList object:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;
public partial class SessionVarTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string item01 = "Item 1";
string item02 = "Item 2";
ArrayList MyArrayIn = new ArrayList();
MyArrayIn.Add(item01);
MyArrayIn.Add(item02);
Session["MyBasket"] = MyArrayIn;
ArrayList MyArrayOut = (ArrayList)Session["MyBasket"];
Debug.WriteLine("Content of ArrayList restored from session var");
Debug.WriteLine("ArrayList item 1: " + MyArrayOut[0]);
Debug.WriteLine("ArrayList item 2: " + MyArrayOut[1]);
}
}
来源:https://stackoverflow.com/questions/11932543/storing-items-for-a-basket-in-session-variables