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?