I have my _Layout.cshtml
file that uses the ViewBag
model to render some dynamic content.
I understand ViewBag
can be populated in the controller and accessed in the view and/or layout page.
My question is, if my Layout page is using @ViewBag.SiteName
, I want to avoid having to set this variable in each controller before I return the view. Is there a way to set this variable on a global level? Or how else should I pass this data to the layout page?
If you set anything in ViewBag - this happens after the Layout has been rendered - You've missed the boat.
As others have mentioned, you can create a "helper" controller:
Then, in your layout: