Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor MVC3 Keeping Global Variable Set on return to view

Tags:

c#

asp.net-mvc

I have a global string variable that I set after one action is made (submit button is pressed) and then I want to access that same string when I press another button, currently I am doing something like

GlobalVariable = "blah";
return View();

What is the best practice for accessing this again. I would like to point out it is the same page(index.cshtml)

Thanks!

like image 927
Badmiral Avatar asked Dec 05 '22 14:12

Badmiral


1 Answers

If its a per user value, use this:

Session["MyKey"] = "MyValue"; // save the value
var myValue = (string) Session["MyKey"]; // retrieve the value

If its one value for all users use this:

Application["MyKey"] = "MyValue"; // save the value
var myValue = (string) Application["MyKey"]; // retrieve the value

Hope this helps.

like image 130
Display Name Avatar answered Dec 10 '22 10:12

Display Name