求ASP.NET购物车实现代码,用SESSION实现的那种
- 提问者网友:萌卜娃娃
- 2021-04-07 10:54
- 五星知识达人网友:酒醒三更
- 2021-04-07 11:04
{
//获取当前点击的商品信息
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)
{
...获取当前点击的商品信息
Shop shop = ShopManager, 1))这个是我自己写的代码;string: public void GetBuyShop(int shopId)
{
/, ShopItem>/.ShopName)
{
shopitem = cart[str];] = cart;
}
else
{
shopitem;cart"
} ShopItem shopitem = null;如果为null;
} Session["
}
} /.aspx".Add(shop,希望对你有帮助.ShopName,表示当前添加的商品早购物车中是不存在的
if (shopitem == null)
{
cart.GetShopByShopId(shopId);获取购物车
Dictionary //,表示存在
if (str == shop;Cart;();~//判断购物车是否存在
if (cart == null)
{
cart = new Dictionary<, ShopItem>.Keys)
{
/, new ShopItem(shop.Redirect(", ShopItem>
/如果相等;
Response;string;cart" cart = Session["] as Dictionary foreach (string str in cart;/);
/
- 1楼网友:毛毛
- 2021-04-07 11:49
这个是我自己写的代码,希望对你有帮助:
public void getbuyshop(int shopid) { //获取当前点击的商品信息 shop shop = shopmanager.getshopbyshopid(shopid); //获取购物车 dictionary<string, shopitem> cart = session["cart"] as dictionary<string, shopitem>; //判断购物车是否存在 if (cart == null) { cart = new dictionary<string, shopitem>(); }
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");
}