119

I added a cshtml page in an project. When I tried to add the following declaration to it, I get an error: "The name 'model' does not exist in current context".

@model xyz.abc.SomeClass

I checked the references, all of them are in place. I added a web.config in view folder, but that didn't fix it.

Is there anything i am missing?

D3vy
  • 831
  • 7
  • 19
user602737
  • 1,295
  • 4
  • 12
  • 12
  • If you are looking for MVC4 solution, the answer from Adam is valid, you need to find the correct version number from your main web.config and use same here. MVC4 Views Folder web.config was missing the line and copying one from root web.config (it might have other values different so check yours) and putting it in Views. Then we closed the project and reload it in Visual Studio and it did the trick. – Farrukh Subhani Jan 19 '13 at 17:58

20 Answers20

239

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!

Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
  • 3
    Doesn't work for me. Or, at least, sometimes I get intellisense and sometimes I don't, even though I have all of this set in the web.config in the Views folder. – Tom Lianza Mar 20 '12 at 17:53
  • You may need to add also a element. See: http://forums.asp.net/t/1701750.aspx/1 – Zé Carlos Dec 03 '12 at 14:09
  • If you're views are in an a non web-project assembly then you might want to check out my answer for the complete contents you need in both web.configs. – Greg Jackman Feb 04 '13 at 20:37
  • I had those entries in my web.config, but after creating new mvc app and copying the one from there it worked fine. - tx! – schmoopy Feb 22 '13 at 00:05
  • Thanks for this, you don't appreciate how much boilerplate the Default MVC template has until you try and build up from an empty one. +1 – JMK Jan 07 '14 at 19:31
  • Same solution works for MVC5, just remember to change the version to 5.0.0.0 in the 'host' line. – c0y0teX Mar 26 '14 at 01:44
  • latest now is 5.1.0.0 - but you're best making a new project and copying it over – Simon_Weaver May 26 '14 at 22:00
  • 9
    make sure to update to latest version (for MVC 5 here) in your `MAIN` web.config file `` – Simon_Weaver May 26 '14 at 22:04
  • What if no one else who works with the solution gets this error, but I do? It wouldn't make sense to change the web.config in that scenario. – Paul Dec 17 '15 at 18:41
  • my case was because I added areas folder manually. lol... see how to add araes folder: http://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-area-in-mvc-5/ – Marco Alves Jul 14 '17 at 19:27
  • Thank you. I fix my problem i suggest to people who meet this error, check your view's web config especially razor and mvc versions. – arslanaybars Aug 21 '17 at 07:35
31

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.

Paul Syfrett
  • 708
  • 9
  • 12
  • 2
    +1 ... I just noticed that for some reason that web.config was not included, glad I found your answer to solve my problem – pollirrata Jul 17 '12 at 20:43
  • 2
    I realize this is old, but I actually had the same problem today, so I wanted to say thanks for this answer. :) – Ricky Mutschlechner Jul 17 '13 at 15:37
  • I had the same problem too, and the similar solution. My `Views/Web.config` was referencing `System.Web.Mvc.MvcWebRazorHostFactory` version 5.0.0.0, and I had a third-party Area referencing v4.0 and with the views cooperating. I set `Views/Web.config` back to v4.0 and everything worked. – Codes with Hammer Feb 14 '14 at 20:51
  • it was specifically your comment about overwriting the web.config with the one from the Views folder that helped me fix this. simply making the contents exactly the same doesn't work for some insane reason. AAARRRGGGHHH! ;) – Ryan Vettese Sep 16 '14 at 01:48
  • In my case, I use robocopy to synchronize local version with server version, excluded all the web.config files. without web.config file in server version, this error will appear. – Will Wu Jul 21 '15 at 09:51
11

If your views are in a class library assembly, which is useful for reuse of shared views among projects, then just doing what Adam suggests might not be enough. I still had issues even with that.

Try this in your web.config in the root of your project:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

And this in the web.config in your views folder:

<?xml version="1.0"?>

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

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

This worked for me. I now have intellisense and no compile errors on my views in a non-MVC project that I can then reference from multiple MVC websites.

tvkanters
  • 3,519
  • 4
  • 29
  • 45
Greg Jackman
  • 686
  • 7
  • 9
  • 1
    Is this for the web.config files in your web project, or the web.config files in your class library? – theJerm May 01 '13 at 20:20
7

I ran into this problem when I inadvertently had a copy of the view file (About.cshtml) for the route /about in the root directory. (Not the views folder) Once I moved the file out of the root, the problem went away.

James Lawruk
  • 30,112
  • 19
  • 130
  • 137
7

Had similar problems using VS2012 and VS2013.
Adding the following line to <appSettings> in the main web.config worked:

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

If the line was already there but said 2.0.0.0, changing it to 3.0.0.0 worked.

  • That was my issue as well after performing an MVC upgrade. It said 2.0.0.0, upon changing to 3, it worked. – FerX32 Mar 20 '17 at 18:48
6

Update: 5/5/2015 For your MVC 5 project you need to set the Version to 5.0.0.0 in your /views/web.config

<system.web.webPages.razor>
     <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</system.web.webPages.razor>
phanf
  • 661
  • 1
  • 11
  • 16
5

While you declare the model at the top of the view using code like this:

@model MyModel

you need to capitalise your references to it below, for example:

@Html.Encode(Model.MyDisplayValue)

I believe a missing web.config in the Views folder would be the major cause of this, but if that is fixed and the problem still persists, check that you are using Model, not model to refer to it in the source.

Bork Blatt
  • 3,308
  • 2
  • 19
  • 17
  • 1
    This solves the error when the issue is that you're trying to reference your model in your own code in the view, like when you make your own helper that does not rely on lambda expressions etc. – JohnC Sep 02 '14 at 18:46
3

I ran into this problem as well. My circumstances were a little different.

  1. One of my view folders was accidentally moved to my project's root directory. This lead to the route project_root/SignUp/ViewName matching before project_root/Views/SignUp/ViewName. This caused the @model error.

  2. After moving the views back into the appropriate location, the directory was somehow recreated in the project root. This time, empty. /SignUp/ViewName worked, but /SignUp/ threw a 403 - Forbidden: Access is denied. error. The entire folder must be removed from the project root.

I hope this helps someone. I spent hours believing the problem was caused by our recent upgrade from MVC 3 to MVC 4. Just a note: Upgrade problems should only occur between MVC 2 and MVC 3. If you see this problem on later versions, it's probably not due to the upgrade.

2

It took me ages to solve this issue, but finally I hope I have solved it on MVC, that is similar:

I have reinstall ASP.NET 4.5 (http://www.asp.net/downloads)

I have followed the upgrading tutorial on http://www.asp.net/whitepapers/mvc4-release-notes

BUT this mentioned paragraph is wrong for me

System.Web.Mvc, Version=4.0.0.0
System.Web.WebPages, Version=2.0.0.0
System.Web.Helpers, Version=2.0.0.0
System.Web.WebPages.Razor, Version=2.0.0.0

Because I have Razor in System.Web.Razor, so I changed the razor namespace to System.Web.Razor.

Add this to your web.config

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

I have add the assembly reference to all these assemblies above

Locate the ProjectTypeGuids element and replace {E53F8FEA-EAE0-44A6-8774-FFD645390401} with {E3E379DF-F4C6-4180-9B81-6769533ABE47}.

That is all.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
2

It is a horrible mistake but:

Be sure you have Web.config files in your View at Remote. Maybe you skipped to upload it by your Ftp programme's filters.

Pcodea Xonos
  • 161
  • 1
  • 6
2

I was missing web.config under Areas/MyArea/Views/web.config. Once added it worked fine. This was with MVC 5 and .NET4.5

Victor
  • 21
  • 1
  • 5
  • Mine was missing as well, with help from this site, i got it working again. https://forums.asp.net/t/1978969.aspx?+Unrecognized+configuration+section+system+web+webPages+razor+ – Perposterer Mar 01 '18 at 23:35
0

There is also another reason. In my case, I had copy an index.cshtml file to web root folder (outside the Views folder) as a backup from remote server.

So, I kept changing my /views/web.config, kept changing my /views/home/index.cshtml and error kept happening... until found out the /index.cshtml outside the views folder, deleted it and sure, it all went back to normal!

Nestor
  • 1,969
  • 4
  • 25
  • 30
0

I ran into this same problem, but in my case, the only thing that had changed is that I had uninstalled Visual Studio 2012 and installed Visual Studio 2013. I opened up our solution, but I kept getting the same The name 'model' does not exist in current context error in every Razor view.

My coworker suggested checking for updates for VS2013. After I installed the VS2013 Update 1, I stopped getting this error.

John Washam
  • 4,073
  • 4
  • 32
  • 43
  • 1
    I'm getting this error with VS2013 Update **2**. With Update 1 it was working fine. Haven't been able to find a solution to it yet. I attempted to uninstall Update 2, but that resulted in VS crashing after opening a cshtml file. – iheartcsharp Jun 27 '14 at 19:30
  • I have the same problem with VS2013 Update 2. – EricP Jul 07 '14 at 19:23
0

Check your config file on views directory. in the add the key for MVC 4/5

sansalk
  • 4,595
  • 2
  • 36
  • 37
0

Reinstalling the nuget solved it for me

PM> Install-Package Microsoft.AspNet.Razor -Version 3.2.3

pixelagent
  • 33
  • 4
0

For me this was the issue. This whole block was missing from the section.

  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  </assemblies>
Kevin Dark
  • 458
  • 5
  • 15
0

I've got the same issue after updating packages. I did the whole stuff You've written above in this topic, but the red underlying of the model keyword has not disappeared. Later, found solution: just deleted 'package' folder from my project's dir and rebuilded, in the meantime allowed NuGet to restore missing packages. Refreshed, and it's done!

Peter.k
  • 1,475
  • 23
  • 40
0

Check your web.config file should be exist in published files

0

I was facing the same issue and then I find a solution. The solution is:

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

The .suo file is a hidden file in the same folder where the .svn solution file exists. Hope, it'll work!

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
0

Closing Visual Studio and re-opening worked for me. Must be a weird bug.

StrattonL
  • 708
  • 1
  • 9
  • 24