I have a list of comment objects in the view. Each comment has one cookie that mark the status from user action. Example: like, spam,...
In the view, I want to read the corresponding cookie of each comment to display the right view to user. Example: user A who liked comment B then the view will display the unlike button
I don't want to read cookie in the controller because the return data is a list of comment objects.
My question is how to read cookie directly in view of MVC3?
In razor view within @{ } block use below code.
string val = "";
if (Request.Cookies["CookieName"] != null) {
val = Request.Cookies["CookieName"].Value;
}
for Read Cookie:
var cookie = Request.Cookies["Key"];
ViewBag.MyCookie= int.Parse(cookie);
and show it in view As:
@ViewBag.MyCookie;
use Request.Cookies
string val = Request.Cookies["CookieName"]?.Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With