Why is this global list resetting to null when called by a GET Method?

Viewed 196

I have a list that is declared globally

public class MyController : Controller
{

    private List<MyViewModel> wages = new List<MyViewModel>();

That is called in the Index GET method like so

public ActionResult Index()
{

if(list is empty)
{
   //Populate List
}
else if(list is NOT empty)
{
  //Display List Contents to User
}
else
{
  //Display 400 error (bad request)
}

}

Every time the above method is called from the browser, the list defaults to null, and has a zero count. As a result, all the data that is calculated in this method and stored in the list is recalculated every time the Index GET Method is called.

From my understanding, the GET method resets all runtime variables to their default state, before the code is executed, and the variables are repopulated with data, but I stand to be corrected. However, this shouldn't be the case considering that the list is not re-instantiated within the Index GET method.

Why is this list resetting to null every time this GET method is called? Surely, the data should be retained within the list given that the list was declared as a global variable, and not a method-wide variable, right?

Is there anything I can do to get around this? Is there anyway to try and persist the data at least so that it can be used elsewhere within the controller, and by extension, the MVC application?

2 Answers

In ASP.NET MVC a Controller is created for every request by the ControllerFactory: https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.defaultcontrollerfactory?redirectedfrom=MSDN&view=aspnet-mvc-5.2

You have to pay attention to ASP.NET life-cycle and if you want to define a list which shouldn't re-create for every request, put your definition line in Global.asax or Startup.cs as a static type:

public static List<MyViewModel> Wages = new List<MyViewModel>(); // in Startup.cs or Global.asax
public ActionResult Index()
{

   if(Startup.Wages is empty)
   {
      //Populate List
   }
   else if(Startup.Wages is NOT empty)
   {
     //Display List Contents to User
   }
   else
   {
     //Display 400 error (bad request)
   }

}

After a lot of deliberation and research, I have resorted to using Session Variables to hold an instance of the list in question, and call said Session variable wherever I need to modify said list within a controller.

I would have used @Saman Gholami's answer, but during my experimentation, I found that the list continues to reset itself when the Index GET method is called, hence why I decided to switch over to Session variables. @Saman's answer was elegant, simple and to the point, but sadly didn't work in the way I wanted it to.

This plus the fact that, based on the links below, Session variables are predominantly loaded and utilized on the Server Side of the application, rather than the Client Side of the application.

Here is the links

how safe is it to use session variables - asp.net / c#

https://forums.asp.net/t/1388630.aspx?Session+Variables+Secure+

Related