Q:

Add to cart functionality with cookies is erasing all contents?

I'm not sure if it's erasing all content on the cookie, or just not fetching the existing cookie from the user then adding to it and returning it back.
Here is the code:
[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");
}

I've used breakpoints and can confirm that if I have an item in the cookie, and then add another different item, the code runs correctly doesn't execute Response.Cookies.Add(new HttpCookie("cart"));. So I don't think I'm creating a new cookie.
In fact, I try adding the same item I'm correctly seeing that the amount is increased for that item instead of having it listed twice.
I think my problem lies in writing to the existing cookie?
Expected result after adding another item:
See two items in the basket page.
Actual result:
See only the latest item I added in the basket page.
Any glaring mistakes? This is my first foray into cookies.

A:

I managed to solve this by using the follow code:
It seemed that adding a single value to the key, caused the rest of the values to disappear. What I did was create a helper method that received the existing cookie, along with the soon-to-be-added productId and quantity.
Below is how I invoke it.
[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");
}

And here is that helper method:
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;
}

