2010-11-02 167 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"));