@ViewBag在当前上下文中不存在。

3
我明白已经有一些在线解决方案了,但是我在尝试所有解决方案后仍然感到困惑。
我只想澄清一下我尝试过的解决方案:
  1. Make sure Microsoft.AspNet.Web.Optimization or System.Web.Optimization is there. I have System.Web.Optimization in References folder. I tried to install Microsoft.AspNet.Web.Optimization in Nuget and the console told me it already exists. I think this means I have it in references already.
  2. Add the namespace to ~/Views/Web.config(not root Web.config). In my ~/Views/Web.config I have:

    <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.Helpers"/>
      <add namespace="System.Web.Mvc"/>
      <add namespace="System.Web.Mvc.Ajax"/>
      <add namespace="System.Web.Mvc.Html"/>
      <add namespace="System.Web.Optimization"/>
      <add namespace="System.Web.Routing"/>
      <add namespace="System.Web.WebPages"/>
     </namespaces>
    </pages>
    </system.web.webPages.razor>
    

    and in my root Web.config:

    <system.web>
     <machineKey configSource="Config\MachineKey.config"/>
     <httpRuntime targetFramework="4.5" maxRequestLength="20480"/>
     <globalization culture="en-CA" uiCulture="en-CA"/>
     <customErrors mode="Off"/>
     <compilation debug="true" targetFramework="4.5">
      <assemblies>
       <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, 
        PublicKeyToken=b77a5c561934e089"/>
       <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, 
        PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
     </compilation>
     <pages buffer="true" enableViewState="false">
      <namespaces>
       <add namespace="System.Web.Helpers"/>
       <add namespace="System.Web.Mvc"/>
       <add namespace="System.Web.Mvc.Ajax"/>
       <add namespace="System.Web.Mvc.Html"/>
       <add namespace="System.Web.Optimization"/>
       <add namespace="System.Web.Routing"/>
       <add namespace="System.Web.WebPages"/>
      </namespaces>
     </pages>
    </system.web>
    

    I searched online and one of the answers told me the namespaces should be the same. I did but nothing changes to me.

  3. Make sure the target version. My target framework is 4.5. My MVC version is 5.2.0.0. Actually I am not understand this solution. I tried to change the framework version to 4.5.1 and the visual studio told me my version is actually 4.5. I guess my target version is OK....(Actually I do not understand this solution)

  4. Add @using System.Web.Optimization to ~/Views/Shared/_Layout.cshtml. This made something happens. My @Scripts.Render is OK now but @ViewBag still has a red line under it.

  5. Delete ComponentModelCache and restart Visual Studio. I am using VS 2015. I tried, nothing happens.

我稍后会更精确地执行这些解决方案,但我想知道是否有其他值得尝试的解决方案。非常感谢您的帮助!!

它是在部署网站后出现的,还是编译器只是说ViewBag不存在? - jegtugado
@JohnEphraimTugado 感谢您的回复。我刚从我们的 TFS 源服务器下载了代码,但还没有部署。在构建后,错误列表面板上显示了这些错误。但是,构建将会成功。 - wadefanyaoxia
4个回答

4
在花费数小时进行探索后,我解决了我的问题。我的解决方案1只能移除红线,并且当我运行本地调试时会给我500错误。 解决方案1:我只是在~/Views/Web.config下面的以下代码中将版本从3.0.0.0更改为2.0.0.0。
  <configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
</sectionGroup>

当我将所有版本更改为2.0.0.0时,红色线条消失了,但我收到了500错误。这意味着system.web.webPages.razor已经正确地加载到_Layout.cshtml中。

据我理解,如果system.web.webPages.razor的版本不正确,则下面在~/Views/Web.config中的设置不会加载:

  <system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.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" />
    <add namespace="System.Web.Optimization" />
  </namespaces>
</pages>
</system.web.webPages.razor>

解决方案2:然后,我检查了根目录下的以下代码部分 Web.config
 <connectionStrings configSource="Config\ConnectionStrings-dev.config" />
<appSettings>
  <add key="webpages:Version" value="2.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="PreserveLoginUrl" value="true" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

webpages:version 的版本是 2.0.0.0。为了与 ~/Views/Web.config 中的版本匹配,我将其更改为 3.0.0.0,然后重新启动 Visual Studio,一切正常运行。

这个链接帮助了我。


0

我曾经在MVC5中犯了一个错误,当我使用模板脚手架生成Razor WebPage UserControl时,我没有使用默认的View/Controller设置进行脚手架搭建。结果什么也没做,但是在Views/主目录下多了两个文件,这让我不太满意。于是我删除了与WebPages相关的两个文件:Views/Web.config和Views/mycontrol.ascx。

之后,我重建了我的解决方案,尝试在VS中调试,但遇到了上述问题。在.cshtml主页面上找不到ViewBag!

我通过再次进行WebPages scaffold来恢复先前生成的Views/Web.config来解决这个问题。然后,我只删除了/Views中的.ascx web控件,保留了Web.config..保存、关闭并重新打开VS,重新构建程序。运行。没有问题。

我使用的是Visual Studio 16.5.4,.NET Framework 4.8和MVC 5.2.7.0。


0
这是一个对我有效的方法:删除EntityFramework NuGet包。在引用中,如果仍然有对"EntityFramework.SqlServer"的引用,请将其删除。然后重新下载EntityFramework。请注意,这不是Entity Framework CORE。

0
请注意,在您的项目文件中加载了一个 Razor 组件,对我来说它是版本 2:
    <Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <Private>True</Private>
  <HintPath>..\packages\Microsoft.AspNet.Razor.2.0.20715.0\lib\net40\System.Web.Razor.dll</HintPath>
</Reference>

在/Views/Web.config中指定了另一个版本,即版本3:
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

当VS cshtml编辑器尝试加载版本3时,它的负载失败。所有razor引用都显示为未知(ViewBag,@Html等)。 最终,当我链接相同的版本时,在cshtml编辑器中运行得很好;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接