在我的MVC 4应用程序中进行了重大重构后,Razor在调试视图时显示了此错误:

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

这是有问题的代码行:

@model ICollection<DataSourceByActive>

我知道@model的用法是正确的。

为什么会这样?我该怎么解决呢?


当前回答

在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" />

其他回答

在我的案例中,问题是在将项目从MVC 4升级到MVC 5之后,我不知为何错过了Views/web.config中的版本更改:

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

它仍然使用旧的2.0.0.0版本。在将版本更改为3.0.0.0之后,一切都开始正常工作。

此外,由于这个问题,Visual Studio 2015 Community Edition每次打开.cshtml文件时都会开始敲打CPU(空闲时占用30-40%)。

我试图添加一个视图,这是我的“视图”文件夹(只是组织我的代码不同,我猜),当我有这个问题。在Views中创建视图(按照惯例)解决了这个问题。

把@Model改成@Model对我来说很管用。

@model表示视图模型对象类型。@Model表示视图模型对象。

不知为何,我的网。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的变量。