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 relati
You should probably use ViewData for this OR use the HttpContext. ViewData will only be valid for your current request so is generally a better alternative than directly using the session object since your data is removed and not available on the next request. Using the session object can suffer from multi request overlap issues.
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
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.
As everyone said global variables here won't work. Even if they were a good idea then they would not work in a web farm scenario as the value would be only on one machine.
Your Index controller action could look something like this....
public ActionResult Index()
{
myModel myStronglyTypedModel = new myModel();
myStronglyTypedModel.SomeStateIWantToKeep = "Hello";
return View(myStronglyTypedModel);
}
Then in your print would do this
public ActionResult Print(myModel myStronglyTypedModel)
{
myStronglyTypedModel.SomeStateIWantToKeep += "Print";
return View("PrintView");
}
And your view could could start with
@ModelType myModel
and so your view would be strongly typed.
I am a vb guy so someone feel free to correct my c#. I suspect the @ModelType directive in vb is @model in c# but haven't checked.
Cheers
You can use TempData Dictionary to save one step calling controller. TempData send your data to just one next session if you want to remain your data to all session you should initialize that TempData in every method controller you want.
public ActionResult Index()
{
if (TempData["MyGlobalVariable"] == null)
return RedirectToAction("MyGlobalVariableInitializer");
else
TempData["MyGlobalVariable"] =(string)TempData["MyGlobalVariable"]+" world";
.
.
.
}
in MyGlobalVariableInitializer, you initialize MyGlobalVariable for the first time.
public ActionResult CompanySelection()
{
return View();
}
public ActionResult MyGlobalVariableInitializer()
{
TempData["MyGlobalVariable"] = "Hello";
return RedirectToAction("Index");
}