Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you keep the value of global variables between different Action Methods calls in MVC3?

I am developing an ASP.NET MVC 3 web application using Razor and C#.

I just discovered that I have some problems with global variables, probably because I am relatively new to MVC.

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:

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);
    }
}

The Print() action method is called with an Html.ActionLink() from the MyView View.

The surprising thing is that the value of _MyGlobalVariable is not kept! So the value of _MyGlobalVariable before the instruction _MyGlobalVariable += "Print" is equal to null.

Am I making some mistake? How can I keep the value of global variables between calls to Views?

Thanks

Francesco

PS: in my specific case the global variable is a Dictionary<K,V> but I guess it does not change the logic.

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 Dictionary<K,V> which usually I don't wrap in ViewModels (I use POCOs or Lists<T>)

like image 599
CiccioMiami Avatar asked May 09 '11 15:05

CiccioMiami


People also ask

How do I create a global variable in razor view?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

How do you avoid global variables?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.


2 Answers

Each request gets a fresh controller instance.
Since your "variable" is an instance field, it doesn't persist.

You can fix that by making it static, or by using application state.

However, don't.
You should never use mutable global state in a web application.

In particular, if you get two request at the same time, your field will get messed up.

Depending on what you're trying to do, you may want to use Session state or cookies.

like image 172
SLaks Avatar answered Sep 28 '22 14:09

SLaks


First of all, there are no such thing as global variables in C#. What you're looking for is a static variable, which can be accessed through a class itself. Since a new controller instance is created on each request, the value of your variable is lost.

You will need to declare your variable as such:

private static string _MyGlobalVariable = "insert default value here";

And access it as such:

MyControllerClass._MyGlobalVariable

Also, this variable will only be accessable from methods of the myController class, as you have it declared private.

In addition to this, you will probably want to lock down the variable whenever using it, as you will end up running into race conditions eventually.

note: I wouldn't suggest doing it this way

like image 38
cwharris Avatar answered Sep 28 '22 12:09

cwharris