2012-04-06 13 views
2

クッキーのすべてのコンテンツを消去しているのか、それとも既存のクッキーをユーザーから取り出して追加して戻したらいいのか分かりません。ここでクッキーを含む機能をカートに追加すると、すべてのコンテンツが消去されますか?

はコードです:

[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"); 
} 

私はブレークポイントを使用していると私はクッキーでアイテムを持っているし、別の異なるアイテムを追加する場合、コードが実行されない正しく動作することを確認することができますResponse.Cookies.Add(new HttpCookie("cart")); 。だから私は新しいクッキーを作っているとは思わない。

実際、同じアイテムを追加しようとしていますが、そのアイテムが2回リストされているのではなく、そのアイテムの量が増えているのを正しく見ています。

私の問題は、既存のCookieに書き込むことにあると思いますか?別の項目を追加した後、結果を期待

はバスケットページに2つの項目を参照してください。

実際の結果:

は、私はバスケットページに追加のみ、最新の項目を参照してください。

目立った間違いはありますか?これは私の最初のクッキーへの進出です。

答えて

0

私は次のコードを使用することによってこの問題を解決するために管理:

キーに単一の値を加算し、値の残りの部分は消えさせたように思われました。私がしたのは、すぐに追加される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"); 
} 

そして、ここでヘルパーメソッドということである。

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

新しいクッキーを毎回作成し、(その後、任意の新しい値を追加新しいクッキーの中に既存の値を読んで)があるはずですすべての値を追加してみてください。 MSDNのドキュメントから

http://msdn.microsoft.com/en-us/library/ms178194.aspx

あなたは直接クッキーを変更することはできません。代わりに、クッキーを変更する は新しい値で新しいクッキーを作成し、クライアントに古いバージョンを上書きするためにブラウザに クッキーを送信することから成ります。

また、Cookieをユーザーのハードドライブに保存しますか?その場合は、クッキーに有効期限を設定する必要があります。

+0

これはトリックをした、私はこの問題を修正するために使用されるコードで自分の質問にお答えします。 –

関連する問題