@ViewBag在当前上下文中不存在 [英] @ViewBag does not exist on current context
问题描述
我知道已经有一些在线解决方案,但是尝试所有解决方案后,我对自己感到困惑.
I understand there are some solutions online already but I am confused by myself after trying all the solutions.
我只想澄清我尝试过的解决方案:
I just want to clarify the solutions which I tried:
- 确保其中有
Microsoft.AspNet.Web.Optimization
或System.Web.Optimization
.我在引用"文件夹中有System.Web.Optimization
.我试图在Nuget中安装Microsoft.AspNet.Web.Optimization
,控制台告诉我它已经存在.我认为这意味着我已经在参考文献中找到了它. -
将名称空间添加到
~/Views/Web.config
(不是rootWeb.config
).在我的~/Views/Web.config
中,我有:
- Make sure
Microsoft.AspNet.Web.Optimization
orSystem.Web.Optimization
is there. I haveSystem.Web.Optimization
in References folder. I tried to installMicrosoft.AspNet.Web.Optimization
in Nuget and the console told me it already exists. I think this means I have it in references already. Add the namespace to
~/Views/Web.config
(not rootWeb.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>
并在我的根目录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.
确保目标版本.我的目标框架是4.5.我的MVC版本是5.2.0.0.其实我不明白这个解决方案.我试图将框架版本更改为4.5.1,而Visual Studio告诉我,我的版本实际上是4.5.我想我的目标版本还可以....(实际上我不理解此解决方案)
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)
将@using System.Web.Optimization
添加到~/Views/Shared/_Layout.cshtml
.这使事情发生了.我的@Scripts.Render
现在可以,但是@ViewBag
下方仍然有一条红线.
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.
删除ComponentModelCache
并重新启动Visual Studio.我正在使用VS2015.我尝试了,没有任何反应.
Delete ComponentModelCache
and restart Visual Studio. I am using VS 2015. I tried, nothing happens.
稍后,我将更精确地执行这些解决方案,但我只想知道是否还有其他我错过并值得尝试的解决方案.非常感谢您的帮助!
I will perform these solutions more precisely later but I just want to know if there are other solutions I missed and worth trying. Thank you so much for the help !!
推荐答案
经过几个小时的挖掘.我解决了我的问题.我的解决方案1 只能删除红线,当我运行Localhost调试时,它给了我500错误.
After a few hours digging. I solved my issue. My solution 1 can only remove the RED LINEs and it gave me 500 error When I run Localhost debug.
解决方案1:我只是在下面的~/Views/Web.config
代码中将版本从3.0.0.0更改为2.0.0.0:
Solution 1: I simply changed the version from 3.0.0.0 to 2.0.0.0 in below code of ~/Views/Web.config
:
<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
.
When I changed all the versions to 2.0.0.0, the red lines went away but I have 500 error. Which means system.web.webPages.razor
loaded to the _Layout.cshtml
correctly.
据我了解,如果system.web.webPages.razor
版本不正确,则不会加载以下我的~/Views/Web.config
设置:
To my understanding, My below settings of ~/Views/Web.config
did not load if the system.web.webPages.razor
version is incorrect:
<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
中的以下部分代码:
Solution 2: Then, I checked below part of code in root 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.我将其更改为3.0.0.0以匹配~/Views/Web.config
中的版本,重新启动Visual Studio,它可以正常工作.
the webpages:version
is 2.0.0.0. I changed it to 3.0.0.0 to match the version in ~/Views/Web.config
, restart visual studio, it worked without any issue.
此链接是帮助我的一个链接.
This link is the one helped me.
这篇关于@ViewBag在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!