asp.net-mvc 当前上下文中不存在名称“ViewBag”——当在“区域”中时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7058344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:24:31  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-3

提问by 010110110101

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

当前上下文中不存在名称“ViewBag”

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:

我运行了升级工具,它确实发现了该区域中的 web.config,但我仍然收到错误消息。我的布局页面非常简单:

<!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>

回答by mpora

You should be aware that when creating an MVC application, you get two Web.Configfiles. 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.

您应该知道,在创建 MVC 应用程序时,您会得到两个Web.Config文件。一个在项目的根目录下,一个在 Views 文件夹下。我遇到了同样的问题,出于某种原因,当我在做我的项目时,我不小心修改了视图下的那个。清理它(Views 文件夹下的那个)修复了这个错误。希望能帮助到你。

enter image description here

在此处输入图片说明

回答by Tae-Sung Shin

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

我有 web.config 在一个区域工作。查看该区域的 View 文件夹中的 web.config 是否有如下部分

 <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>

回答by GentryRiggen

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...

检查以确保位于“视图”目录中的 web.config 文件位于该目录的根目录下。我不小心把它移到了“共享”目录,然后把我的头发扯了一段时间,直到我弄明白了……