41

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?

mmcdole
  • 91,488
  • 60
  • 186
  • 222

6 Answers6

55

In short... Use the controller's view bag.

ViewContext.Controller.ViewBag.MyVar = "myVal";

and

@ViewContext.Controller.ViewBag.MyVar

===============================================================

There is good information here: http://forums.asp.net/post/4254825.aspx

===============================================================

Generally, ViewData["StoreName"] is same as ViewBag.StoreName

Also, Controller.ViewData["StoreName"] = Controller.StoreName = ViewContext.Controller.ViewBag.StoreName =ViewContext.Controller.ViewData["StoreName"]

But every view and partial view gets its own instance of viewdata.

http://jeffreypalermo.com/blog/viewdata-mechanics-and-segmentation-excerpt-from-asp.net-mvc-in-action/

===============================================================

There is a another solution here: https://stackoverflow.com/a/4834382/291753

===============================================================

Community
  • 1
  • 1
Malgaur
  • 1,842
  • 15
  • 17
4

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:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@Html.Partial("Title")

~/Shared/Title.cshtml:

@{
 ViewBag.Title = "bytecourse - Online Courses in Technology"; 
}
Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
Raj
  • 262
  • 1
  • 8
  • In my mvc5 project, ViewBag values which has been from partial view is not accessible from main view. Same problem with ViewData Even if I render my partial directly into layout page – Alexus1024 Oct 09 '14 at 07:59
4

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.

just.jimmy
  • 954
  • 1
  • 8
  • 15
3

You could use sections in your _Layout if you want to set a default title:

<title>
    @if (IsSectionDefined("Title"))
    {
        @RenderSection("Title")
    }
    else
    {
        @:bytecourse - Online Courses in Technology
    }
</title>

and inside views you could override it:

@section Title {
    Overriden title
}

One more reason not to use ViewBag :-)

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Hi @Darin Dimitrov, is not used `@RenderSection("Title")` is used `@RenderSection("Title", required:=false)` in case of, do you have a need requirement IsSectionDefined("Title") ?? – B. Bilgin Jun 20 '12 at 09:40
  • @BeratBilgin it depends if you want a default value or not if that section is not defined by the view. – Adam Tuliper Sep 02 '13 at 07:06
0

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

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
0

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?

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71