Reputation: 6588
In ASP.NET MVC, how can I create a single cookie and save multplie values to it?
Thanks!!
Upvotes: 0
Views: 3307
Reputation: 141
document.cookie = "myCookie=myValue";
document.cookie = "myOtherCookie=myOtherValue";
Upvotes: 0
Reputation: 14827
var cookie = new HttpCookie();
cookie.Values["FirstKey"] = value1;
cookie.Values["SecondKey"] = value2;
or a shortcut:
cookie["ThirdKey"] = value3;
HttpCookie.Values is a NameValueCollection, a class that appears in many places in ASP.NET MVC. Read this for a good tutorial on how to use it to its full potential.
Upvotes: 6
Reputation: 52430
This problem can be rephrased as: "How can I store multiple values in a single string?"
It's up to you to come up with a system. If the values are simples numbers or strings, use a separator like a ";" and then split later.
Upvotes: 2