可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答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!
回答2:
I ran into this same issue when I created an new area to organize my pages. My structure looks kind of like:
WebProject
- [] Areas
- [] Controllers
- [] Views
- 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 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
- [] Controllers
- [] Views
- Web.config
I ran into this because I manually created this new area using Add -> New Folder to add the folders. I should of right clicked the project and selected Add -> Area. Then Visual Studio would have taken care of setting the area up correctly.
回答3:
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.
回答4:
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.
回答5:
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.
回答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>
回答7:
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.
回答8:
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.
回答9:
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.
回答10:
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.
回答11:
I was missing web.config under Areas/MyArea/Views/web.config. Once added it worked fine. This was with MVC 5 and .NET4.5
回答12:
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!
回答13:
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.
回答14:
Check your config file on views directory. in the
add the key for MVC 4/5
回答15:
Reinstalling the nuget solved it for me
PM> Install-Package Microsoft.AspNet.Razor -Version 3.2.3
回答16:
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>
回答17:
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!
回答18:
Check your web.config file should be exist in published files
回答19:
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!