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

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

这是有问题的代码行:

@model ICollection<DataSourceByActive>

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

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


当前回答

确保在你的网站上都有以下内容。配置和视图目录Web。在appSettings部分配置

<add key="webpages:Version" value="2.0.0.0" />

MVC5使用:

<add key="webpages:Version" value="3.0.0.0" />

(而且它只存在于主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>

我有同样的问题,我创建了一个新的项目,并复制了网络。在Gupta的回答中推荐配置文件,但这并没有为我解决问题。我检查了Alex和Liam的答案,我认为这一行一定是从新网页上复制的。config,但它看起来像新项目本身没有这一行(MVC5):

<add key="webpages:Version" value="3.0.0.0" />

添加一行到views/web。配置文件解决了我的问题。

确保在你的网站上都有以下内容。配置和视图目录Web。在appSettings部分配置

<add key="webpages:Version" value="2.0.0.0" />

MVC5使用:

<add key="webpages:Version" value="3.0.0.0" />

(而且它只存在于主Web中。配置文件)。

我通过使用@Model而不是仅仅在打印变量时使用model解决了这个问题。

您可能会在代码中使用一个名为model的变量。