ChatGPT解决这个技术问题 Extra ChatGPT

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

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?

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.

A
Adam Terlson

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!


I can't mark this as the answer, but this was my problem too. Forgot to add these sections to the web.config.
Yeah, wish I could mark yours as the answer. I was stuck on this for a while (gave up after my first attempt to convert to Razor failed), and this was the problem all along. Thanks!
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.
Thank you very much! It safed my day!
make sure to update to latest version (for MVC 5 here) in your MAIN web.config file <add key="webpages:Version" value="3.0.0.0" />
P
Paul Syfrett

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.


+1 ... I just noticed that for some reason that web.config was not included, glad I found your answer to solve my problem
I realize this is old, but I actually had the same problem today, so I wanted to say thanks for this answer. :)
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.
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! ;)
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.
t
tvkanters

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.


Is this for the web.config files in your web project, or the web.config files in your class library?
J
James Lawruk

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.


Thank you! I have just been going mad with the same problem and this was the answer ;-)
Cool! I remember going mad as well. :)
I had some .cshtml files in a folder in the root directory, that caused only folder in my views to fail... while(true) { bashHeadToWall; }. Thanks.
佚名

Had similar problems using VS2012 and VS2013. Adding the following line to 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.
p
phanf

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>

Yup found version 5.2.0.0 which is causing the issue but changing it to 5.0.0.0 fixed it!!! Thanks.
B
Bork Blatt

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.


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.
佚名

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

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


T
Tomas Kubes

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.


appsettings addition to webconfig solved it for me. Though I think <add key="webpages:Version" value="2.0.0.0" /> is the driving factor, the other setting isn't needed.
for MVC 5 this needs to be 3.0.0.0 but yes this is very important!
P
Pcodea Xonos

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.


V
Victor

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


Mine was missing as well, with help from this site, i got it working again. forums.asp.net/t/…
N
Nestor

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!


J
John Washam

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.


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.
I have the same problem with VS2013 Update 2.
s
sansalk

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


p
pixelagent

Reinstalling the nuget solved it for me

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


K
Kevin Dark

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>

P
Peter.k

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!


A
Arash Farahani

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


A
Arsman Ahmad

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

Close Visual Studio Delete the SUO file Restart Visual Studio

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


S
StrattonL

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