17

My problem is very similar to this problem, except mine is in a sub-Area (right click, Create Area)

The name 'ViewBag' does not exist in the current context

I ran the upgrade tool and it did discover the web.config in the area, but I still get the error. My layout page is very simple:

<!DOCTYPE html>

<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

And my content page looks like this near the top:

@model IEnumerable<ProjectName.Models.OrderViewModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/_AdminLayoutPage.cshtml";
}

<h2>Index</h2>
Community
  • 1
  • 1
101010
  • 14,866
  • 30
  • 95
  • 172

3 Answers3

19

You should be aware that when creating an MVC application, you get two Web.Config files. One in the root of the project and one under the Views folder. I had the same issue and for some reason when I was working on my project, I accidentally modified the one under the Views. Cleaning it (the one under the Views folder) fixed that error. Hope it helps.

enter image description here

mpora
  • 1,411
  • 5
  • 24
  • 65
  • My Web.config wasn't included as a solution file. So it was running fine on my machine but not on my generated publish. Including in solution fixed, ty. – talles Feb 14 '14 at 18:19
11

I have web.config working in an area. See if your web.config in your View folder of the area has s part like following

 <configSections>
 <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>
</system.web.webPages.razor>
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
3

Check to make sure that the web.config file located in your "Views" directory is located at the ROOT of that directory. I accidentally moved it to "Shared" directory and tore my hair out for a while until I figured this out...

GentryRiggen
  • 788
  • 10
  • 10