title: How do you keep the value of global variables between different Action Methods calls in MVC3?
body: <p>I am developing an ASP.NET MVC 3 web application using Razor and C#. </p>
  
  <p>I just discovered that I have some problems with global variables, probably because I am relatively new to MVC.</p>
  
  <p>I have a controller with some global variables and action methods. I declared a global variable in order to allow the action methods to manipulate it and reflect the manipulations to all the action methods. I have the following situation:</p>
  
  <pre><code>public class myController : Controller
  {
      private string _MyGlobalVariable;
  
      public ActionResult Index()
      {
         _MyGlobalVariable = "Hello";
         //other code
         return View("MyView");
      }
  
      public ActionResult Print()
      {
         _MyGlobalVariable += "Print";
  
         return View("PrintView", _MyGlobalVariable);
      }
  }
  </code></pre>
  
  <p>The <strong>Print()</strong> action method is called with an <strong>Html.ActionLink()</strong> from the <strong>MyView</strong> View.</p>
  
  <p>The surprising thing is that the value of _MyGlobalVariable is not kept! So the value of <strong>_MyGlobalVariable</strong> before the instruction <code>_MyGlobalVariable += "Print"</code> is equal to null.</p>
  
  <p>Am I making some mistake? How can I keep the value of global variables between calls to Views?</p>
  
  <p>Thanks</p>
  
  <p>Francesco</p>
  
  <p>PS: in my specific case the global variable is a <code>Dictionary&lt;K,V&gt;</code> but I guess it does not change the logic.</p>
  
  <p>PPS: I know you can use ViewModels instead of global variables to pass data between action methods but in my case is much less code if I use a <code>Dictionary&lt;K,V&gt;</code> which usually I don't wrap in ViewModels (I use <strong>POCOs</strong> or <code>Lists&lt;T&gt;</code>)</p>