Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'ViewBag' does not exist in the current context -- When in an "Area"

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>
like image 996
101010 Avatar asked Aug 14 '11 17:08

101010


People also ask

Is the name ViewBag does not exist in the current context?

The name 'ViewBag' does not exist in the current contextIt's probably a sign that you are running on an older version of ASP.NET MVC (or a pre-release of MVC 3). You can download the latest version of ASP.NET MVC 3 here.

How use ViewBag in external js file?

Access Viewbag Value in External JavaScript file, It will throw an error ExternalJs. js:1 Uncaught SyntaxError: Invalid or unexpected token. So, create a JavaScript global variable and store the ViewBag and ViewData value in that global variable. Then, access that JavaScript variable in external JavaScript.


1 Answers

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

like image 116
2 revs Avatar answered Sep 19 '22 21:09

2 revs