Razor View throwing "The name 'model' does not exist in the current context"
After significant refactoring in my MVC 4 application, and Razor shows this error while debugging Views:
The name 'model' does not exist in the current context.
This is the offending line of code:
@model ICollection<DataSourceByActive>
I know that the usage of @model
is correct.
Why is this happening? How can I fix it?
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:
- Close Visual Studio
- Delete the SUO file
- 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.