Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys

I have set the value in the cookie but when I access that value in the viewbag from another view controller I can't find that value.
I would really appreciate you help

thanks

What I have tried:

I have tried to set cookie value in c#
Posted
Updated 1-Aug-16 21:09pm
v2
Comments
Suvendu Shekhar Giri 1-Aug-16 9:42am    
Share the code what you have tried.
Richard Deeming 1-Aug-16 10:18am    
Cookies and the ViewBag are two completely different places. Data stored in cookies doesn't magically appear in the ViewBag, and data stored in the ViewBag doesn't get stored in cookies.

Click "Improve question" and post the relevant parts of your code.

Here's how I have been managing the cookies in MVC
C#
[HttpGet]
public ActionResult Details()
{
  HttpCookie cookie = new HttpCookie("MySiteCookie", "For the love of Pete, do not put unencrypted info in cookies!");
  Response.SetCookie(cookie);
  return View();
}

[HttpPost]
public ActionResult Details(int i)
{
  HttpCookie cookie = Request.Cookies["MySiteCookie"];
  // do something with the cookie
  return View(new Model.SomeData(i));
}
 
Share this answer
 
Cookies are one of several ways to store data about web site visitors during the time when web server and browser are not connected.

check this useful resources about set cookie value in C#

C# .NET: Get and Set Cookie Values in .NET[^]

Cookies in ASP.Net C#[^]

Cookies in ASP.Net C#[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900