Я использовал этот пример для создания корзины покупок: http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/
Это хороший пример, он хранит корзину покупок в состоянии Session ["cart"] и все должно работать нормально.
НО это не так.Событие, если закрыть браузер или попробовать другие браузеры, он все еще сохраняет состояние?!?!
Вот конструктор + метод добавления в корзину:
public List<CartItem> Items { get; private set; }
// Readonly properties can only be set in initialization or in a constructor
public static readonly ShoppingCart Instance;
// The static constructor is called as soon as the class is loaded into memory
static ShoppingCart()
{
// If the cart is not in the session, create one and put it there
// Otherwise, get it from the session
if (HttpContext.Current.Session["MPBooksCart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session["MPBooksCart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["MPBooksCart"];
}
}
// A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }
public void AddItem(int book_id)
{
// Create a new item to add to the cart
CartItem newItem = new CartItem(book_id);
// If this item already exists in our list of items, increase the quantity
// Otherwise, add the new item to the list
if (this.Items.Contains(newItem))
{
foreach (CartItem i in Items)
{
if (i.Equals(newItem))
{
i.Quantity++;
return;
}
}
}
else
{
newItem.Quantity = 1;
Items.Add(newItem);
}
}
Можете ли вы дать советв чем может быть проблема?
Я читал около 2 часов о состоянии сеанса, и везде говорится, что он должен быть изменчивым при закрытии броузера, но в этом случае это не так.
С уважением, Алекс