The name model does not exist in the current context

I was upgrading an EPiServer CMS project from 8.8.2 to 8.11.0.

The project was created using Visual Studio 2012, and I wanted to upgrade it to Visual Studio 2015, .NET 4.5 -> .NET 4.6, and ASP.NET MVC 4 -> ASP.NET MVC 5.2.3

Nuget update went fine, I didn't get any compile time errors, but when I ran the project, I got a runtime exception:

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'H:\Users\dejan\AppData\Local\Temp\Temporary ASP.NET Files\vs\481d66d7\80da9e62\assembly\dl3\69a42dcc\5995682a_e2e3d001\System.Web.WebPages.Razor.dll'.

When ASP.NET MVC is updated using nuget, the powershell script will only update the main web.config file while Views / web.config remains unchanged.

To fix the error, I had to manually change the version of System.Web.WebPages.Razor from 2.0.0.0 to 3.0.0.0 in Views / web.config:

<configSections>
  <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>
</configSections>

to

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

After that, the web app was running fine but Visual Studio complained that @model does not exist in the current context, Html.RenderPartial was not recognized anymore, etc:

Again, the problem was in Views / web.config. This time I had to change all instances of System.Web.Mvc, Version=4.0.0.0 to System.Web.Mvc, Version=5.2.3.0, restart the Visual Studio, and rebuild the project.

If that doesn't help, you should also try to delete files under C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache

comments powered by Disqus