English 中文(简体)
除了用厨师做织物的功能外,其他功能正在消除所有内容?
原标题:Add to cart functionality with cookies is erasing all contents?

我不敢肯定的是,它是否抹去了 co的所有内容,或者仅仅不把现有的 co子从用户中挑出来,然后添加到这里并退回。

该守则是:

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

我使用了断点,可以证实,如果我在 co子里有一个项目,然后添加另一个不同的项目,则该代码正确操作了<代码>Response。 Cookies.Add(new HttpCookie(“cart”));。 因此,我不认为我会创造新的厨师。

事实上,我试图增加同一个项目Im,正确地看到,该项目的数额是增加的,而不是将其列入两次。

我认为,我的问题在于给现有的厨师写信?

在增列另一个项目后预期结果:

在篮子页上看到两个项目。

实际结果:

仅见篮子上增加的最新项目一。

任何明显的错误? 这是我第一次到会。

最佳回答

我通过使用以下守则解决这一问题:

似乎对钥匙增加单一价值,导致其他价值消失。 我确实创造了一种得到现有 co子的更贴现的方法,以及即将产生的附加产品。 页: 1

以下是我是如何援引的。

[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;
}
问题回答

Try establishing a new cookie 每一次添加“应当达到的数值”(将现有数值纳入新的宇宙库,然后添加任何新的数值)。

From the MSDN documentation, http://msdn.microsoft.com/en-us/library/ms178194.aspx

You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client.

你们也想把 co子坚持到用户的艰苦努力中去? 如果是的话,你必须在 co子上规定一个到期日。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签