In my application I have some basic user information that needs to be displayed on every page (name, profile img). At the moment I have simply set the model in the _Layout
Depending on when you want to generate the ViewBag data you could also use the functions OnActionExecuting or OnActionExecuted. This might be more suitable because some data might not be available at the time the Controller is created.
public class MyController : Controller
{
//Executes before every action
protected override void OnActionExecuting(ActionExecutedContext context)
{
//Call the method from the base class
base.OnActionExecuting(context);
//Create the ViewBag data here
ViewBag.XYZ = XYZ();
}
//Executes after every action
protected override void OnActionExecuted(ActionExecutedContext context)
{
//Call the method from the base class
base.OnActionExecuted(context);
//Create the ViewBag data here
ViewBag.XYZ = XYZ();
}
}