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

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

这是有问题的代码行:

@model ICollection<DataSourceByActive>

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

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


当前回答

以下是我所做的:

关闭Visual Studio 删除SUO文件 重启Visual Studio

“.suo”文件是一个隐藏文件,与“.svn”解决方案文件位于同一文件夹中,包含Visual Studio用户选项。

其他回答

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

在我的情况下,我在foreach开始时丢失了@

    @foreach (var item in Model)
    {
        <tr class="clickable-row">
            <td class="clickable-field">
                @Html.DisplayFor(modelItem => item.Label)
            </td>
            <td class="clickable-field hidden-xs">
                @Html.DisplayFor(modelItem => item.Value)
            </td>
        </tr>
    }

对我来说,问题是我最近导入的一个库中的. net版本冲突。我导入的库是为4.5.2和ASP。NET MVC站点我把它导入了目标4.5。重新编译后说库4.5网站将编译。

此外,没有编译错误,但这个问题被报告为“警告”。因此,如果有任何警告,请务必阅读所有警告。

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

以我为例,我最近从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相关的正确升级版本。