Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on 

Need to use a cookie with MVC3

I have an MVC3 application that uses JavaScript and jQury. I need to use a cookie to record certain values from the last user's session. So I have a few questions:

1) If the data is to be stored in the database, via the Model, is there any reason for a cookie?
2) Can I do 100% of the coe in JavaScript and just leave the data on the user's disk as a cookie with no involvement of the Model?

Please provide the code to do this in JavaScript, in the event I decide to do this client side and not involve the Model.

Thanks.
ASP.NETJavaScriptC#

Avatar of undefined
Last Comment
curiouswebster
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

You could do it either way. If you're using a membership provider that handles remembering who the user is (identifying them) then you probably wouldn't need to save the data on the client.

A recent project needed to remember the user w/o a provider in the mix, so we used a cookie.

There are tons of examples on writing a cookie from js so I won't bother posting the links. A quick search will get you there.

You can also write a cookie from the controller like this:

Response.Cookies["mycookie"].Value = "myvalue";
Avatar of curiouswebster
curiouswebster
Flag of United States of America image

ASKER

Ok, thanks. It seems that creating a cookie in the controller saves the effort of coding methods in the Model and needing to create a new table, and possible sprocs, right?

What other code have you got handy for creating, writing to and reading from the cookie, using MVC?
ASKER CERTIFIED SOLUTION
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of curiouswebster
curiouswebster
Flag of United States of America image

ASKER

Thanks!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo