在我的MVC 4应用程序中进行了重大重构后,Razor在调试视图时显示了此错误:
当前上下文中不存在名称“model”。
这是有问题的代码行:
@model ICollection<DataSourceByActive>
我知道@model的用法是正确的。
为什么会这样?我该怎么解决呢?
在我的MVC 4应用程序中进行了重大重构后,Razor在调试视图时显示了此错误:
当前上下文中不存在名称“model”。
这是有问题的代码行:
@model ICollection<DataSourceByActive>
我知道@model的用法是正确的。
为什么会这样?我该怎么解决呢?
当前回答
以我为例,我最近从MVC 4更新到MVC 5,这把web搞砸了。配置很糟糕。这篇文章帮助很大。
http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
底线是你需要检查你的网页中所有的版本号引用。config和Views/web。配置,以确保他们引用了与MVC 5相关的正确升级版本。
其他回答
我找到了解决办法。 如果你想更新razor版本或mvc 4到5,改变一些行。
Views/web.config中的旧代码
<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>
取而代之的是
<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>
sectionGroup也必须更改。
在web中更改下面的行。配置视图文件夹解决了同样的错误。
From
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
To
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
不知为何,我的网。config在oldVersion属性中设置0.0.0.0:
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</runtime>
改变到1.0.0.0是解决方案:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
您可能会在代码中使用一个名为model的变量。
以下是我所做的:
关闭Visual Studio 删除SUO文件 重启Visual Studio
“.suo”文件是一个隐藏文件,与“.svn”解决方案文件位于同一文件夹中,包含Visual Studio用户选项。