Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor View throwing "The name 'model' does not exist in the current context"

I think you have messed up the web.config file which lives in the Views folder.

Create a new project targeting the same .NET framework and copy its Views/web.config file on top of the one in your current project. This will fix your problem.

Also, as Dudeman3000 commented, if you have Areas in your MVC project they all have Views\web.config files too.


Make sure you have the following in both your site Web.config and views directory Web.config in the appSettings section

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

For MVC5 use:

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

(And it only exists in the main Web.config file.)


Here is what I did:

  1. Close Visual Studio
  2. Delete the SUO file
  3. Restart Visual Studio

The .suo file is a hidden file in the same folder as the .svn solution file and contains the Visual Studio User Options.


I had the same issue, I created a new project and copied the web.config files as recommended in the answer by Gupta, but that didn't fix things for me. I checked answer by Alex and Liam, I thought this line must have been copied from the new web.config, but it looks like the new project itself didn't have this line (MVC5):

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

Adding the line to the views/web.config file solved the issue for me.


Changing following line in web.config of view folder solved the same error.

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