I have a C# MVC 5
web application which I have created a model that contains categories and then I used the automatically created controller and views via MVC 5 Controller with views, using Entity Framework
.
I have several other controls that need a list of those categories. The list of categories is needed at least once per page but sometimes multiple times. Instead of doing multiple database calls, I just read the information into a list, like this:
var categoryQuery = from d in db.Categories
orderby d.Name
where d.Visible == true
select d;
List<Category> categoryList = new List<Category>();
categoryList.AddRange(categoryQuery);
Is there somewhere in my controller that I can put this code so that it is called only once but then I can use the list multiple times?
Edit:
Editing to provide a better example. In my _Layout.cshtml page, I call an HTML action which returns a partial view to display the category list:
@Html.Action("Menu", "Categories", new { DropdownMenuTitle = "Categories" })
Then on my Index.cshtml page, in the same controller, I do another database call to get a list of the categories which I create a SelectList and then a DropDownList on the page.
How do I do this without the two database calls?