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?
This question is related to
asp.net-mvc
asp.net-mvc-4
razor
I've found a solution. If you want to update razor version or mvc 4 to 5, change some lines.
Old code in Views/web.config
<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>
Replaced with
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
sectionGroup
must be change, too.
In my case I was missing @ at the beginning of the foreach
@foreach (var item in Model)
{
<tr class="clickable-row">
<td class="clickable-field">
@Html.DisplayFor(modelItem => item.Label)
</td>
<td class="clickable-field hidden-xs">
@Html.DisplayFor(modelItem => item.Value)
</td>
</tr>
}
In order to solve this I made sure that I upgraded to the newest MVC version using NuGet and Package Manager Console.
Install-Package Microsoft.AspNet.Mvc -Version 5.2.4
Then upgraded to the latest Razor version
Install-Package Microsoft.AspNet.Razor -Version 3.2.4
Then I changed all the web.config files to reflect the change. As you will see below:
In the main web.config file, make sure that the webpages:version is correct. This is where it can be found (ignore the other keys):
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
</configuration>
Then look for the other versions listed in the assemblies, check the version of the assembly against the version of the library listed in your project references! You may not need all of these.
<system.web>
<compilation debug="true" targetFramework="4.6">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Helpers, Version=3.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=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
Runtime assemblyBinding should show the "newversion" as well, see where it reads NewVersion 5.2.4.0? But also check all the other versions.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
THEN in the Views Web.Config section, make sure that Razor is the correct version:
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<configuration>
And Lastlt there is the Pages section of the Views Web.Config
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
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.)
In my case, the following code founds to be useful. Place below code in Web.config file under Views folder.
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
Once the code is updated, make sure to clean and rebuild the solution. I hope this will help you out!
if you take this problem without any change on your project as like as me, you need change your web.config that placed in View Folder. just write new line by Enter or Remove an empty line . then save your web.config and rebuild. my problem solved with this solution
In my case, I recently updated from MVC 4 to MVC 5, which screws up the web.config pretty badly. This article helped tremendously.
The bottom line is that you need to check all your version number references in your web.config and Views/web.config to make sure that they are referencing the correct upgraded versions associated with MVC 5.
Changing to @Model from @model did the job for me.
@model represents the View Model object type. @Model represents the View Model object.
I was using an MVC4 project with Visual Studio 2019 - and it turned out VS 2019 does not support MVC 4 out-of-the-box. You have to install this.
Steps:
NOTE: Required for visual studio to be closed
You are likely to use in the code a variable named model
.
I solved the problem by using @Model
instead of just model
when printing the variables.
None of the existing answers worked for me, but I found what did work for me by comparing the .csproj
files of different projects. The following manual edit to the .csproj
XML-file solved the Razor-intellisense problem for me, maybe this can help someone else who has tried all the other answers to no avail. Key is to remove any instances of <Private>False</Private>
in the <Reference>
's:
<ItemGroup>
<Reference Include="Foo">
<HintPath>path\to\Foo</HintPath>
<!-- <Private>False</Private> -->
</Reference>
<Reference Include="Bar">
<HintPath>path\to\Bar</HintPath>
<!-- <Private>True</Private> -->
</Reference>
</ItemGroup>
I don't know how those got there or exactly what they do, maybe someone smarter than me can add that information. I was just happy to finally solve this problem.
Changing following line in web.config of view folder solved the same error.
From
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
To
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
For some reason my web.config had 0.0.0.0 in the oldVersion attribute:
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</runtime>
changing to 1.0.0.0 was the solution:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
For me, the issue was a conflicting .NET version in one of the libraries that I recently imported. The library I imported was compiled for 4.5.2 and the ASP.NET MVC site I imported it into targeted 4.5. After recompiling said lib for 4.5 the website would comppile.
Also, there were no compilation errors, but the issue was being reported as a "warning". So be sure to read all warnings if there are any.
Here is what I did:
The .suo file is a hidden file in the same folder as the .svn solution file and contains the Visual Studio User Options.
I had the same issue, I created a new project and copied the web.config files as recommended in the answer by Gupta, but that didn't fix things for me. I checked answer by Alex and Liam, I thought this line must have been copied from the new web.config, but it looks like the new project itself didn't have this line (MVC5):
<add key="webpages:Version" value="3.0.0.0" />
Adding the line to the views/web.config file solved the issue for me.
I had the same problem when deploying to an Azure App Service
In my case it was because ~/Views/Web.config wasn't included in the project.
It worked in IIS Express but when I deployed to azure, I got the same error. By not being included in the .csproj file, it wasn't deployed.
The solution was to ensure ~/Views/Web.config is included in the project.
If you go to solution explorer and click the "Show all files" icon, then open up Views you might see an unincluded Web.config file under there.
Add it in, re-publish, and bob's your uncle.
There appear to be 3 version-number settings which need to be correct in relation to each other here:
... System.Web.Mvc, Version=x.x.x.x ...
(in various places ~\Views\web.config)... System.Web.WebPages.Razor, Version=x.x.x.x ...
(in various places ~\Views\web.config)<add key="webpages:Version" value="x.x.x.x" />
(in ~\web.config) NB: root web.configCombinations that have worked for me:
Combination 1:
System.Web.Mvc, Version=4.0.0.0
System.Web.WebPages.Razor, Version=2.0.0.0
<add key="webpages:Version" value="2.0.0.0" />
Combination 2:
System.Web.Mvc, Version=5.2.7.0
System.Web.WebPages.Razor, Version=3.0.0.0
<add key="webpages:Version" value="3.0.0.0" />
A final observation is that the webpages:Version
setting appears to be optional. Removing it appears to have no negative consequences, at least in the context of the issue at hand.
I was trying to add a view which were outside of my "Views" folder (just to organize my code differently, I guess), when I had this issue. Creating the view inside Views (as by convention) solved it.
In my case, the issue was that after upgrading the project from MVC 4 to MVC 5 I somehow missed a version change in the Views/web.config:
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
It still had the old 2.0.0.0 version. After changing the version to 3.0.0.0 everything started working just right.
Also, because of this problem, Visual Studio 2015 Community Edition would start bashing the CPU (30-40% usage at idle) every time I would open a .cshtml file.
In my case, I removed web.config file from Views folder by accident. I added it back , and it was OK.
Source: Stackoverflow.com