I have the default _ViewStart.cshtml in my /Views folder. I'd like to be able to access my ViewBag object so I can set the default title for all my views.
However, with:
@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
ViewBag.Title = "bytecourse - Online Courses in Technology";
}
I get "The name 'ViewBag' does not exist in the current context" as a runtime error.
What do I need to do?
You could use sections in your _Layout if you want to set a default title:
and inside views you could override it:
One more reason not to use
ViewBag
:-)You can achieve this using Partial views. Put all your Title related common code in a Partial View called
Title.cshtml
in the shared folder. In the_viewstart
simply call the Partial view._ViewStart.cshtml:
~/Shared/Title.cshtml:
hmm, you can access ViewBag via ViewData, e.g.
ViewContext.ViewData["Title"]
.So if you set ViewBag data in an action filter for example, you can pull it out from _ViewStart.cshtml using
ViewContext.ViewData["Title"]
.But I tried to assign a value using
ViewContext.ViewData["Key"] = value;
and it doesn't seem to persist to the actual view.You can create one layout page which is using your Viewbag data and add layout to your blank _ViewStart page it will work
on ViewStart.cshtml
In short... Use the controller's view bag.
and
===============================================================
There is good information here: http://forums.asp.net/post/4254825.aspx
===============================================================
===============================================================
There is a another solution here: https://stackoverflow.com/a/4834382/291753
===============================================================
Its not 100% clean but see a workaround using PageData or a bit of enumeration at:
How do I set ViewBag properties on _ViewStart.cshtml?