The name 'model' does not exist in current context in MVC3

Solution 1:

Update: If you are using a newer version of MVC, the same process applies, just be sure to use the correct version number in the web.config's <host> line.

Well, I found myself experiencing the same thing you did, and after a bit further research, I found out what the problem is!

You need to include the default MVC3 web.config for the Views folder. MVC3 has two: one in the root for your application, and one for the views folder. This has a section for included namespaces. Be sure that yours looks something like this:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

I suggest that you create a new MVC3 project, then just copy the web.config created for you into your views folder.

Important Once you've done that, you need to close the file and reopen it. Voila! Intellisense!

Solution 2:

I ran into this same issue when I created a new area to organize my pages. My structure looked like:

WebProject

- [] Areas
    - [] NewArea
        - [] Controllers
        - [] Views
- [] Controllers
- [] Views
    - Web.config
- Web.config

The views created in the Views folder under the WebProject worked fine, but the views created under the NewArea threw the following error:

The name 'model' does not exist in the current context.

To fix this I copied the web.config in the Views folder under the WebProject to the Views folder in the NewArea. See below.

WebProject

- [] Areas
    - [] NewArea
        - [] Controllers
        - [] Views
            - **Web.config**
- [] Controllers
- [] Views
    - Web.config
- Web.config

I ran into this because I manually created this new area using Add -> New Folder to add the folders. I should have right-clicked the project and selected Add -> Area. Then Visual Studio would have taken care of setting the area up correctly.