2010-11-02 169 views
0

我有这个问题,直到现在我找不到答案。删除,添加更多会话值到中继器

我创建了一个自定义购物车应用程序,并且当我试图解决最终页面时出现问题。这个购物车模型就像一个向导,这意味着在进入最后一个(购物车)页面之前会有页面被传递。

这里的问题是,在开始页面中检查/选择的每个值都保存在Session(Session [“CurrentCartItem”])中。并且在最终购物车页面中,“CurrentCartItem”会话中的收集值被插入到“中继器”中。

现在的问题是,

  • 我怎么能在转发(这是再返回到开始页)没有这表明在转发走了价值增加更多的价值?
  • 如何从中继器中删除其中一个值?

仅供参考,我将使用Session而不是数据库保存所有值。

请任何人都可以帮助我解决这个问题。也许对于其他的,这是一个简单的问题,但对我来说是没有答案的问题... :-)

在此先感谢前...

回答

0

你应该在会话中存储的项目列表,这样就可以轻松修改它。简单的代码片段将是

// class for representing item within cart 
public class ShoppingCartItem { ... } 

// helper method to get shopping cart from session 
public static List<ShoppingCartItem> GetShoppingCart() 
{ 
    var cart = HttpContext.Current.Session["ShoppingCart"] as List<ShoppingCartItem>; 
    if (null == cart) 
    { 
    cart = new List<ShoppingCartItem>(); 
    HttpContext.Current.Session["ShoppingCart"] = cart; 
    } 
    return cart 
} 


// Add to cart 
var item = new ShoppingCartItem(); 
// initialize item 
var cart = GetShoppingCart(); 
cart.Add(item); 
// Add to cart snippet 

将购物车(列表)绑定到中继器以显示其中的项目。

// remove via index (say 4th item) 
var cart = GetShoppingCart(); 
cart.RemoveAt(3); 

// remove via some item property (say product code 'XYZ') 
cart.RemoveAt(cart.FindIndex(c => c.ProductCode == "XYZ"));