4

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!

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
Badmiral
  • 1,549
  • 3
  • 35
  • 74
  • A global variable in any ASP.NET (MVC) application will be available to every user of that application. So user A could set the varible, then user B could set it to something else, then user A could press the other button to access it, but it now has the value that user B set. Is that what you are looking for? – Colin Mackay Sep 14 '12 at 14:35
  • Session variable may work for this. –  Sep 14 '12 at 14:39
  • I am not sure if this is what you really want. However, if you really need an application wide variable (and most likely you don't) use `Application` object: `HttpContext.Application["myID"] = "blah";` – Zdeslav Vojkovic Sep 14 '12 at 14:39

6 Answers6

8

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.

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • 1
    Session is full of issues around scalability and management, not going to -1 this one since it is a workable solution, but I would not recommend it. – Maess Sep 14 '12 at 18:10
  • Still better than `ViewBag` :). Also, using `Session`, they (Microsoft) will probably address those issues in next releases, and you won't have to redo any of your code. THanks for noting this out. – Display Name Sep 14 '12 at 18:35
4

Are you really sure that you want every user of the site to share this state? This is quite a rare requirement. More likely it should be specific to the user.

There are few places in ASP.NET where you can save information:

  • Application - global scope, so the value is visible to all users.
  • Session - session scope, visible to one user
  • Cache - global scope, so the value is visible to all users.
  • TempData - visible to one user, during this and next request

Examples:

public ActionResult MyAction()
{
    HttpContext.Application["global_var"] = "anyone can see me";
    HttpContext.Cache["global_cached"] = "anyone can see me, but I can expire";
    Session["session_var"] = "only this user can see me";
    TempData["flash_var"]= "only this user can see me but also in next request";        
}
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • This is really bad advise for MVC3 , FYI there is built in session variables called TempData it is very lightweight and short lived passed to the next controller action ONLY. MVC is not to be used like Web Forms. This is shown in all the MVC books from version 1 to version 4 (yes there is currently 1 version 4 book out that I own) Caching is an attribute built in. Your answer does not show a understanding of MVC, but other than TempData is general advise for an ASP.NET WEB FORMS application. http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications – Tom Stickel Sep 14 '12 at 19:58
  • mmm, sorry, have you actually read the answer? what advice did I give, except the one against a global variable? I enumerated the possibilities and let the guy choose. What's not true about this usage of cache? but at least we now know that you own a book. – Zdeslav Vojkovic Sep 14 '12 at 20:35
  • 1
    Yes, you should read more books. Caching with MVC [OutputCache(Duration = 3600, VaryByParam = "none")] TempData is the only thing related to MVC , the rest of web forms. Cheers. Books are on sale. I can sell you my old ones. – Tom Stickel Sep 14 '12 at 21:03
  • 2
    Please study MVC and build real world application with MVC before giving advise clearly suited for mostly webforms. "Razor MVC3 Keeping Global Variable Set on return to view" is the question your answer implies very little understanding of what MVC applications are put together. I had 20 software engineers review and concur with me. Please stop embarrassing yourself. – Tom Stickel Sep 14 '12 at 23:12
  • 2
    Your cache is globally visible to all users, LOL Yikes I give up LOL – Tom Stickel Sep 15 '12 at 06:14
  • 1
    Of course it is. Did you have problems understanding: "anyone can see me, but I can expire" - or tell them to fix it: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.cache.aspx – Zdeslav Vojkovic Sep 15 '12 at 06:16
3

I am not a fan of the ViewBag, so my solution would be to add it to the view model you are passing to the view, and use a HiddenFor() so that it gets posted back if you need it for each subsequent action.

Maess
  • 4,118
  • 20
  • 29
1

putting it in the ViewBag.GlobalVariable

Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
0

Instead of using Session or even viebag in ASP MVC i would recomend in this case to use Cashing

 Cache["permvaluetostore"] =valuetokeep
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

You need to persist that data. You may use either a database table or Session variable to do so.

Session variable value will be available to the current session .

If you want the value to be a global across all sessions, you may also consider using Caching.

Shyju
  • 214,206
  • 104
  • 411
  • 497