I have a layout with an extension method that takes a list of menu items and creates HTML from that. In the controller actions I have:
public ActionResult Article(string rk)
{
Viewbag.menuItems = _menu.MenuItems("00");
..
return View(vm);
}
I have this repeated in many actions. Then in the _layout I have:
Html.NavLinks(ViewBag.menuItems)
I realize we’re supposed to give the view what’s needed but in this case would it be better if the view pulled partial details from an action? I’m asking this because I have only ONE layout that needs the data and I thought it may be better to code there rather than have the data retrieved in 20+ actions and sent to the views.
If it did a pull from the layout then would MVC cache the results of the action?
If pulling results from an action was better then how exactly could I do it?
It wouldn’t cache the results of the action no, it would go and get them on every request.
Instead of “pulling them from an action”, just put them in the model that you are sending back from the controller, then you can use them in your view.