I have a controller that should display several selects (dropdowns) with predefined values.
I display a list of Records, each Record can have a predefined Theme and correspond to a City from the list of predefined values.
In my controller I have
private IEnumerable<Record> records;
private static IEnumerable<Theme> themes;
private static IEnumerable<City> cities;
private async Task<bool> LoadThemes()
{
themes = await repository.GetTableEntitiesAsync<Theme>(lang);
return true;
}
private async Task<bool> LoadCities()
{
themes = await repository.GetTableEntitiesAsync<City>(lang);
return true;
}
and the action
public async Task<IActionResult> Index()
{
// records = from DB, then
await LoadThemes(); ViewData["themes"] = this.themes;
await LoadCities(); ViewData["cities"] = this.cities;
return View(records);
}
public async Task<IActionResult> Edit(string id)
{
// record => from id, then
await LoadThemes(); ViewData["themes"] = this.themes;
await LoadCities(); ViewData["cities"] = this.cities;
return View(record);
}
Since I can't do a async constructor, nor I am sure passing via Index view, how can I initialize my "static" collections only once?
cities = await...truedoesn't make sense either.