在我的MVC 4应用程序中进行了重大重构后,Razor在调试视图时显示了此错误:
当前上下文中不存在名称“model”。
这是有问题的代码行:
@model ICollection<DataSourceByActive>
我知道@model的用法是正确的。
为什么会这样?我该怎么解决呢?
在我的MVC 4应用程序中进行了重大重构后,Razor在调试视图时显示了此错误:
当前上下文中不存在名称“model”。
这是有问题的代码行:
@model ICollection<DataSourceByActive>
我知道@model的用法是正确的。
为什么会这样?我该怎么解决呢?
当前回答
我找到了解决办法。 如果你想更新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文件。
<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>
</configSections>
一旦代码更新,请确保清理并重新构建解决方案。我希望这对你有所帮助!
我试图添加一个视图,这是我的“视图”文件夹(只是组织我的代码不同,我猜),当我有这个问题。在Views中创建视图(按照惯例)解决了这个问题。
我在部署到Azure应用程序服务时遇到了同样的问题
在我的例子中,是因为~/Views/Web。配置没有包含在项目中。
它在IIS Express中工作,但当我部署到azure时,我得到了同样的错误。由于没有包含在.csproj文件中,所以它没有被部署。
解决方案是确保~/Views/Web。配置包含在项目中。
如果您转到解决方案资源管理器并单击“显示所有文件”图标,然后打开视图,您可能会看到一个未包含的Web。配置文件在那里。
把它加进去,重新出版,就万事大吉了。
不知为何,我的网。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>