2012-04-06 32 views
2

我不確定它是刪除cookie上的所有內容,還是不從用戶獲取現有的cookie,然後添加並返回。添加到購物車帶有Cookie的功能正在刪除所有內容?

下面是代碼:

[Authorize] 
public ActionResult AddToCart(int productId, int quantity) 
{ 
    //If the cart cookie doesn't exist, create it. 
    if (Request.Cookies["cart"] == null) 
    { 
     Response.Cookies.Add(new HttpCookie("cart")); 
    } 

    //If the cart already has this item, add the amount to it. 
    if (Request.Cookies["cart"].Values[productId.ToString()] != null) 
    { 
     int tmpAmount = Convert.ToInt32(Request.Cookies["cart"].Values[productId.ToString()]); 
     Response.Cookies["cart"].Values.Add(productId.ToString(), (quantity + tmpAmount).ToString()); 
    } 
    else 
    { 
     Response.Cookies["cart"].Values.Add(productId.ToString(), quantity.ToString()); 
    } 

    return RedirectToAction("Index"); 
} 

我用的斷點,可以確認的是,如果我在cookie中的項目,然後添加其他不同的項目,代碼運行正常不執行Response.Cookies.Add(new HttpCookie("cart")); 。所以我不認爲我正在創建一個新的cookie。

事實上,我嘗試添加相同的項目,我正確地看到該項目的金額增加,而不是列出兩次。

我覺得我的問題在於寫入現有的cookie?添加另一個項目後

預期結果:

看到在籃下頁面兩項。

實際結果:

見只有我在籃下頁面添加的最新項目。

任何明顯的錯誤?這是我第一次進入cookies。

回答

0

我設法通過如下代碼來解決這個問題:

看來,增加一個價值的關鍵,引起了其餘值消失。我所做的是創建一個接收現有cookie的幫助器方法,以及即將添加的productId和數量。

以下是我如何調用它。

[Authorize] 
public ActionResult AddToCart(int productId, int quantity) 
{ 
    //If the cart cookie doesn't exist, create it. 
    if (Request.Cookies["cart"] == null) 
    { 
     Response.Cookies.Add(new HttpCookie("cart")); 
    } 

    //Helper method here. 
    var values = GenerateNameValueCollection(Request.Cookies["cart"], productId, quantity); 
    Response.Cookies["cart"].Values.Add(values); 

    return RedirectToAction("Index"); 
} 

這裏是helper方法:

private NameValueCollection GenerateNameValueCollection(HttpCookie cookie, int productId, int quantity) 
{ 
    var collection = new NameValueCollection(); 
    foreach (var value in cookie.Values) 
    { 
     //If the current element isn't the first empty element. 
     if (value != null) 
     { 
      collection.Add(value.ToString(), cookie.Values[value.ToString()]); 
     } 
    } 

    //Does this product exist in the cookie? 
    if (cookie.Values[productId.ToString()] != null) 
    { 
     collection.Remove(productId.ToString()); 
     //Get current count of item in cart. 
     int tmpAmount = Convert.ToInt32(cookie.Values[productId.ToString()]); 
     int total = tmpAmount + quantity; 
     collection.Add(productId.ToString(), total.ToString()); 
    } 
    else //It doesn't exist, so add it. 
    { 
     collection.Add(productId.ToString(), quantity.ToString()); 
    } 

    if (!collection.HasKeys()) 
     collection.Add(productId.ToString(), quantity.ToString()); 

    return collection; 
} 
2

嘗試創建一個新的cookie每次和增加所有的值,應該是有(讀取現有的數據到新的cookie,然後添加任何新的值)。

從MSDN文檔,http://msdn.microsoft.com/en-us/library/ms178194.aspx

您不能直接修改的cookie。相反,更改Cookie 包括創建一個包含新值的新cookie,然後將cookie發送到瀏覽器以覆蓋客戶端上的舊版本。

您是否還希望cookie堅持用戶的硬盤?如果是這樣,您必須在Cookie上設置到期日期。

+0

這並獲得成功,我會回答與我用來解決這個問題的代碼我自己的問題。 – 2012-04-06 15:11:54

相關問題