求ASP.NET购物车实现代码,用SESSION实现的那种

2025-02-23 16:45:00
推荐回答(2个)
回答1:

这个是我自己写的代码,希望对你有帮助: public void GetBuyShop(int shopId)
{
//获取当前点击的商品信息
Shop shop = ShopManager.GetShopByShopId(shopId);
//获取购物车
Dictionary cart = Session["cart"] as Dictionary;
//判断购物车是否存在
if (cart == null)
{
cart = new Dictionary();
} ShopItem shopitem = null;
//判断当前添加的商品在购物车中是否村
foreach (string str in cart.Keys)
{
//如果相等,表示存在
if (str == shop.ShopName)
{
shopitem = cart[str];
}
} //如果为null,表示当前添加的商品早购物车中是不存在的
if (shopitem == null)
{
cart.Add(shop.ShopName, new ShopItem(shop, 1));
}
else
{
shopitem.Count = shopitem.Count + 1;
} Session["cart"] = cart;
Response.Redirect("~/Cart.aspx");
}

回答2:

参考下微软的petshop,那里很详细的