In the same way as in another ASP.NET application - via Cookies property on
Request and Response objects. These objects are accessible from controller via
HttpContext.Request and HttpContext.Response properties. So just use
HttpContext.Request.Cookies and HttpContext.Response.Cookies from your controller.
Don't forget to click "Mark as Answer" on the post that helped you.
Yeah but how do i pre-populate the field? Example if a user clicks "Remember my username" on the login form, I want to store the name in a cookie and prefill it next time the user visits the form. I can create and retrive a cookie but not sure how to pre
fill the username field.
Just store username into HttpContext.Response.Cookies collection and when you are preparing data for showing login form then read
username from HttpContext.Request.Cookies.
Don't forget to click "Mark as Answer" on the post that helped you.
xoail
Member
2 Points
48 Posts
ASP.Net MVC and Cookies
Oct 19, 2009 01:06 PM|LINK
I want to pre populate some of the form fields from browser cookies on the mvc website. How can I do that? How to set and load cookies in an mvc app?
Augi
Contributor
6730 Points
1142 Posts
Re: ASP.Net MVC and Cookies
Oct 19, 2009 01:38 PM|LINK
In the same way as in another ASP.NET application - via Cookies property on Request and Response objects. These objects are accessible from controller via HttpContext.Request and HttpContext.Response properties. So just use HttpContext.Request.Cookies and HttpContext.Response.Cookies from your controller.
xoail
Member
2 Points
48 Posts
Re: ASP.Net MVC and Cookies
Oct 20, 2009 12:35 PM|LINK
Yeah but how do i pre-populate the field? Example if a user clicks "Remember my username" on the login form, I want to store the name in a cookie and prefill it next time the user visits the form. I can create and retrive a cookie but not sure how to pre fill the username field.
Augi
Contributor
6730 Points
1142 Posts
Re: ASP.Net MVC and Cookies
Oct 20, 2009 12:45 PM|LINK
Just store username into HttpContext.Response.Cookies collection and when you are preparing data for showing login form then read username from HttpContext.Request.Cookies.
xoail
Member
2 Points
48 Posts
Re: ASP.Net MVC and Cookies
Oct 20, 2009 02:03 PM|LINK
How to read data and show on screen? any code snippet?
xoail
Member
2 Points
48 Posts
Re: ASP.Net MVC and Cookies
Oct 20, 2009 09:21 PM|LINK
bump.. kinda urgent :(
KeFang Chen ...
Star
8329 Points
852 Posts
Re: ASP.Net MVC and Cookies
Oct 21, 2009 06:48 AM|LINK
Hi,
Please refer to the following code to display the cookie .
And we can use the following code to add a cookie.
public ActionResult Index() { HttpCookie hc = new HttpCookie("test","123"); Request.Cookies.Add(hc); return View(_db.MovieSet.ToList()); }khoailang
Member
2 Points
15 Posts
Re: ASP.Net MVC and Cookies
Nov 06, 2010 03:37 PM|LINK
hi, just a quick query please
It appears cookies does not persist in localhost server, does it? (using built in server of VS)
I did set start up url as www.abc.com in my solution setting and my IE also has cookies setting enabled.
Please advise how can I have cookies work in localhost testing server?
Thanks