Reputation: 14084
In Global.asax.cs, I have a List of 5 Cars (The Model, Cars.cs
, pulls data from the database):
public class MvcApplication : System.Web.HttpApplication
{
public static List<Car> Cars = Enumerable.Range(1,5)
.Select(i => new Car(i))
.ToList();
protected void Application_Start()...
}
Then I use the list on my front page:
@foreach(var C in MySite.MvcApplication.Cars)
{
<a href="/[email protected]">@C.Name</a>
}
I can't figure out how to force the Cars variable to refresh. The quickest way I have found is to log into the server, open IIS, and Stop and Start the website - a simply restarting it doesn't work. Obviously this is not sustainable, and I'm sure there's a better way...
Here's some other things I've tried:
I need a way to automate this, so a user can update info and refresh the data without needing me to login to the server every time something changes.
Upvotes: 0
Views: 142
Reputation: 1643
Break the assignment out into a method and call that method once in app start and as needed on your "refresher" page. This will set you up for good DI should you need to start going down that route.
public static List<Car> Cars;
public static void GetCars()
{
Cars = Enumerable.Range(1,5)
.Select(i => new Car(i))
.ToList();
}
protected void Application_Start()
{
GetCars();
}
Then call Global.GetCars()
in response to UI events on your page.
Upvotes: 1