André Miranda
André Miranda

Reputation: 6588

How can I create a single cookie and save multiple pairs key/values to it?

In ASP.NET MVC, how can I create a single cookie and save multplie values to it?

Thanks!!

Upvotes: 0

Views: 3307

Answers (3)

devendra tata
devendra tata

Reputation: 141

document.cookie = "myCookie=myValue";
document.cookie = "myOtherCookie=myOtherValue";

Upvotes: 0

Talljoe
Talljoe

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

x0n
x0n

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

Related Questions