118

I'm starting to develop in ASP.NET again and I ran into a small error within Visual Studio. My .cshtml files show errors when using a few razor functions. For example "The name 'ViewBag' does not exist in the current context". Here is a picture:

screenshot of problem in visual studio 2015

I am using a demo project. You can find the project here: https://github.com/Wintellect/Angular-MVC-Cookbook/tree/master/BasicProject

I have looked through several other threads and most suggest to update the web.config file(s). These 2 config files are already present and since it's a pretty popular demo I assume it has all the required configuration in it. I have of course looked through these config files and they do indeed include the suggested solutions.

Other details:

  • I have already used clean & rebuild on the solution but that changed nothing.
  • When I create a completely new MVC project it does work
  • My friend has the same problem and we both use VS 2015 and Windows 10
  • I can still run the application and it does work.
starball
  • 20,030
  • 7
  • 43
  • 238
Michjuh
  • 1,182
  • 2
  • 8
  • 10
  • When it still runs it's just an error in the background compiler. Try deleting the .suo file. – H H Mar 28 '15 at 16:16
  • which project are you running ? basic ? crud, routing ? – Prashant Ghimire Mar 28 '15 at 16:36
  • 1
    @Henk unfortunately that did not work. – Michjuh Mar 28 '15 at 16:57
  • @Prashant I have tried both the basic project and the routing project. The issue occurs in both of them. – Michjuh Mar 28 '15 at 16:59
  • Did you solve it ? I had same problem @Michjuh – Burk Jul 31 '15 at 08:22
  • @Burk Hey sorry for the late response but no I did not manage to solve this problem. It's no problem for most projects so I stopped trying eventually. I have not tried Benjamin Anderson's answer and it's something I haven't done before so that might work. – Michjuh Aug 20 '15 at 10:40
  • @Michjuh I learned why it happens. Just answered the question. – Burk Aug 23 '15 at 12:12
  • I didn't understand the real situation but in my case I went in the Properties selected a previous target framework (from 4.6 to 4.5.2, recompiled it and all disappeared. Then I returned to 4.6 doing the same thing. – Gianpiero Aug 30 '15 at 07:52
  • @Michjuh did you find any solution ? i m facing same problem – Shan Khan Oct 04 '15 at 18:35
  • VS2017, MVC 5.2.3.0 - nothing on this page solved my problem. I still have this problem. Same code works on another person's computer. So the issue must be somewhere on my computer. – Vanity Slug - codidact.com Jan 17 '20 at 16:22

20 Answers20

143

I had this issue despite having all the correct configuration.

It turned out to be some bad files in the Component Cache, preventing the Razor views from recognising ViewBag, Model, and HtmlHelpers. Deleting these files solved the problem (good versions of these files were created next time I opened Visual Studio).

The files are located here:

%LOCALAPPDATA%\Microsoft\VisualStudio\14.0\ComponentModelCache

Delete all four files:

  • Microsoft.VisualStudio.Default.cache
  • Microsoft.VisualStudio.Default.catalogs
  • Microsoft.VisualStudio.Default.err
  • Microsoft.VisualStudio.Default.external

I have subsequently seen the same issue on several other developer machines and this fix quickly solves it.

jpmc26
  • 28,463
  • 14
  • 94
  • 146
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 15
    Awesome, this fixed the issue for me. I've also found that deleting your Temporary ASP.NET Files also helps. C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files – Kevin Nov 16 '15 at 22:40
  • 1
    This fixed my issue caused by upgrading .net 4 to 4.5.2 and upgrading mvc 3 to 5 – Joe Phillips May 12 '16 at 17:50
  • My issue was that my model's properties were not being recognized. This fix solved my problem immediately. – jkerak May 21 '16 at 17:42
  • this may be a workaround but the real problem at least for me, always turned out to be an error in other units not nearly related to this issue, once the error got resolved also this errors about the bag disappear. Moral; resolve all other errors and when build all, clear all first, you should never delete manually those cache files – Santos Jan 30 '18 at 06:33
  • This worked for me BUT only if I removed the code causing the error in my view before deleting the cache files – lucky.expert May 17 '18 at 21:36
  • To clarify my solution: I had this error after renaming a class and namespace which confuses VS at times for some reason. I tried deleting the cache files and restarting VS but didn't work. But, it did work if I removed all code in my view with the red squiggleys first, including the viewbag line. I saved, closed VS, deleted the cache files, restarted VS and now it all works fine! – lucky.expert May 18 '18 at 14:11
  • 7
    my folder is 15.0_7e8de76c\ComponentModelCache for VS 2017. – Kevin .NET Jul 31 '18 at 17:48
  • 3
    This solution is perfect for VS 2019 too, don’t forget to close VS and then opening later so that cache is built on proper versioning. Also, MVC version should be same in views/web.config and main web.config. same rule for System.Web.WebPages.Razor. – youngseagul May 15 '20 at 14:54
  • Worked; I didn't do the delete - just stopped/restarted VS (2022, v 17.4.0). – Gary Huckabone Dec 02 '22 at 19:33
  • 2
    Look like is uses more or less the same template (XX.0_XXXXXXXXX\ComponentModelCache) in this case16.0_277b98eb\ComponentModelCache. Cleared the folder. Restarted VS. Issue Resolved. – Danie Schoeman Apr 14 '23 at 10:32
48

Update the version numbers in the settings of the web.config file in the Views folder.

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

If you have updated the MVC version through nuget, should be:

 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Other dependency versions might need to be updated too. Compile and namespace issues in Views for MVC helpers and objects are typically related to messed up web.config files within the View folders.

Benjamin Anderson
  • 1,376
  • 10
  • 15
  • 13
    This worked for me, although I had to enter a version number of 5.2.0.0 instead of 5.2.2.0. You can see what version you have by going to References > System.Web.MVC and selecting Properties. Thanks for the help – user2233949 Jul 15 '15 at 22:23
  • @user2233949, @Benjamin I'm using MVC version `4.0.0.0`, I could see it from properties. What should be the fix in my case? – Zeeshan Aug 06 '15 at 04:42
  • Check all of your web.config files and make sure all of them are using the correct host version. Sometimes when you have overridden settings in over view folders the problem can slip by without being corrected. – Benjamin Anderson Aug 06 '15 at 19:01
  • 1
    I tried all the other suggestions but this one worked for me using 5.2.3.0. Thanks Benjamin – Mitch Nov 11 '16 at 21:04
  • Thanks! This fixed it for me as well. – Shane Jun 07 '17 at 03:40
  • Also consider updating the versions for `System.Web.WebPages.Razor` within sectionGroup and `System.Web.Mvc` within page section – SergejK Jun 05 '19 at 11:46
  • 2
    This was the correct answer for me. Also after changing the web.config, do not forget to close all your `*.cshtml` and `*.vbhtml` files and clean&rebuild the solution. VS 2019 16.1.2 – Štěpán Jun 14 '19 at 14:22
  • Finally, the answer! I went through the web.config in my views folder and found other incorrect versions. Remember to determine which version to use, go to each dll referenced in the web.config in your References folder. I also had to change this section: ` – goku_da_master Sep 17 '19 at 04:25
27

I accidentally removed web.config from the Views folder, thinking that it was unnecessary. When I put it back it started working.

Jordan Rieger
  • 3,025
  • 3
  • 30
  • 50
InGeek
  • 2,532
  • 2
  • 26
  • 36
23

Find "webpages:Version" in the appsettings and update it to version 3.0.0.0. My web.config had

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

and I updated it to

<add key=”webpages:Version” value=”3.0.0.0″ />
Damian Green
  • 6,895
  • 2
  • 31
  • 43
15

After trying everything under the sun, it turns out something had modified my System.Web.WebPages.Razor <sectionGroup> in /Views/Web.Config from the proper CamelCase System.Web.WebPages.Razor to an all-lowercase system.web.webpages.razor which ultimately was my demise.

Hoping this may help some other poor soul with this problem...

For reference, this is the correct entry (for my scenario)

<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"> 
jbwebtech
  • 424
  • 6
  • 11
7

i faced this issue more times and finally i discovered that it happened because the folder of views should contain it is own web config

hosam hemaily
  • 412
  • 5
  • 17
  • 2
    But what's supposed to be in that web.config? Shouldn't it be autogenerated? _(update)_ Found what it should have and how it's generated https://stackoverflow.com/questions/29305914/which-nuget-package-creates-the-web-config-in-your-views-folder/29306208 – drzaus Jan 08 '18 at 22:39
  • Yes, you are right, in my project, Web.Config didn't existed earlier ( yes it should generate automatically but took a project from previous developer, not sure why he deleted it). After adding Web.Config file for Views folders it worked. – Vikas Lalwani Jul 21 '20 at 06:27
6

Sometimes it is not enough to change version numbers in the settings of the web.config. This problem occurs because Visual Studio 2015 doesn't have mvc4 tooling.

Solution is to upgrade your project to MVC 5. It totally solves the problem. You can get help from this page. It clearly explains it.

http://www.asp.net/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

By the way Visual Studio team declared they will add mvc4 support to Visual Studio 2015 with the first update.

Burk
  • 2,969
  • 1
  • 23
  • 24
  • I checked all the steps from the article mentioned in the URL. And found some inconsistencies. Fixing those solved the issue. – miha Mar 13 '23 at 20:10
5

In my case I had this issue happening only on my PC only for one project. My project worked fine in VS2012 but when opened in VS2017 it was having this problem.

The main issue for me here was cache. My project was pointing to the older cached versions of dll for Razor and MVC and it was not finding features that were added in newer versions, such as ViewBag. ViewBag was added in Razor version 3, but because I had Razor version 2 cached it could not find ViewBag (but it could find other stuff which were present in version 2). However, simply clearing cache like in the accepted answer did not fix my problem. We had to change config files to say "use version 3 instead of 2" for Razor and "use version 5 instead of 4" for MVC, then closing VS, removing cache, and opening project and rebuilding. Then things got fixed. Below are more detailed instructions.

Here is the sequence of changes that helped me fix my problem:

  1. In "Views" folder's Web.config change all locations where
    • MVC version is set to 4.0.0.0 to 5.0.0.0
    • Razor version is set to 2.0.0.0 to 3.0.0.0

Here are those lines in my case:

<sectionGroup name="system.web.webPages.razor" type="... Version=3.0.0.0 ...">
    <section name="host" type="... Version=3.0.0.0 ..." .../>
    <section name="pages" type="... Version=3.0.0.0 ..." .../>
</sectionGroup>
...
<host factoryType="... System.Web.Mvc, Version=5.0.0.0 ..." />
...
<pages
    ...
    pageParserFilterType="... Version=5.0.0.0 ..."
    pageBaseType="... Version=5.0.0.0 ..."
    userControlBaseType="... Version=5.0.0.0 ...">
    <controls>
        <add assembly="... Version=5.0.0.0 ..." ... />
    </controls>
</pages>

(notes: 1) your versions may be different, I am just telling what needed to be done in my case 2) I omitted some stuff with "..." for brevity 3) even if you have MVC version something like 5.3.2.0 you should still input 5.0.0.0, same with Razor's version - input all zeros in 3.0.0.0)

  1. In main config file (the one at the top level) change webPages:version from 2.0.0.0 to 3.0.0.0.

  2. This change probably did not affect solution but I'll still mention it. Make sure that "Views" folder's <namespaces> section has the exact same contents as main .conifg file at the root of the project, something like:

<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.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
</namespaces>
  1. Finally, close VS. Delete all cache files in %LOCALAPPDATA%\Microsoft\VisualStudio\14.0\ComponentModelCache (or whatever your correct path is) just like it is suggested in (currently) accepted answer by Fenton. Also delete .scan file, even if it is not mentioned in that answer. Once you are done, open VS, Clean your project and Rebuild it. Things should be working now. It worked for me.
  • 1
    Thank you. This worked for me for my MVC 4 application after upgrading to MVC 5.For whatever reason, these files did not get edited with the new version numbers. This should be the top answer. – Jakal Mar 10 '21 at 15:31
  • 1
    +1 Although I realised (by reading some of the higher voted answers) that it was the `sectionGroup` versions that needed to be increased in my case, this answer actually highlights that fact. – Quinton Smith Jan 31 '23 at 22:34
2

late to the game but none of these solutions worked for me.

I upgraded to VS 2017, and the site worked, but Visual Studio compiler broke most of .cshtml files

TLDR;

mysolution.sln had the wrong version

sln diff

so you want to create a new solution with newer VS and compare it to yours and copy version info over.

full upgrade steps:

  1. ensure .sln file has correct version for VS
  2. right click MySolution in VS > Nuget Manager > Updates > run all updates
  3. right click MySolution in VS > Application > update Target Framework (4.5.2 in my case)
  4. fix any version issues in .config files (i.e. RAZOR to 3.0.0.0)
  5. fix any code issues in compiler (for me ForEach was ambiguous)
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
  • My project was started in VS2013, when I hit VS2017 I started seeing these errors but everything compiled ok still so I tried to figure out what was going on but not too hard. It was becoming an issue because of opening a view created a large number of errors. I am now on VS2019 and stumbled on your fix; you rock! My .sln had 14.0.25123.0 and was to be 16.3.9. I also fixed any version issues with the framework. I didn't have to update nuget packages (thankfully) as I don't have time to fully test all the updates. – Zonus Nov 21 '19 at 23:17
1

I've wasted well over a working day on this. I tried everything in this (and other) posts, to no avail. Eventually I found that I needed to open the project in a different way. I've just upgraded an existing website to Visual Studio 2015, MVC5, .NET framework 4.5.2 and am using Windows 10, but suspect the answer would have been the same for other versions of each software application.

So this is the menu option I was choosing to open a website:

enter image description here

Instead of this, I opened the csproj file defining the project using this menu option:

enter image description here

I then had the "clean solution" option many other people refer to:

enter image description here

However, I didn't even need this - everything just worked! Well, I got on to the next set of problems, anyway ...

Andy Brown
  • 5,309
  • 4
  • 34
  • 39
1

I had added the controller by

  • Step 1: Add > Class
  • Step 2: UserController
  • .
  • .
  • .

corresponding code created:

namespace SampleApp.Controllers
{
    public class UserController {
    }
}

My errors got changed by changing the Step 1 as:

  • Step 1: Add > Controller
  • Step 2: User Controller
  • .
  • .
  • .

corresponding code created:

namespace SampleApp.Controller {
    public class UserController : Controller {
    }
}

The problem was: I had not Inherited my class from Controller

Other messages in my error included - all of which were from this problem:

error CS0117: 'ModelState' does not contain a definition for 'IsValid'

error CS0103: The name 'RedirectToAction' does not exist in the current context

error CS0103: The name 'View' does not exist in the current context

error CS0103: The name 'ViewBag' does not exist in the current context

error CS0103: The name 'Json' does not exist in the current context

Hope this change helps those who have not paid attention in their initial steps.

1

After having try at least 90% of every solution/trick of this subject (and others), I've finally solved it!! Spoiler alert, there is no trace of logic is the following so-called "solution": Running the solution with all tabs closed finally worked, when nothing else would have spare me the "ViewBag&Co does not exist in the current context"... You've been warned, no freaking logic!

Hope it'll help someone one day ^^

Jueverhard
  • 111
  • 11
0

Turning it off and on again worked for me. You can try restarting visual studio.

kravits88
  • 12,431
  • 1
  • 51
  • 53
0

I was able to resolve the issue by adding the following to the top of my .cshtml page

@{ViewBag.Title = "Title";}

After recompiling I was able to delete this line and the error was gone.

Colin
  • 1,758
  • 1
  • 19
  • 24
0

I faced similar issue with one of my view. In my case I just cleaned up the solution and tried to rebuild it and its works.

0

the following link can help you.

It will show you what could be the problem

it solve my problem. somehow i had delete my web.config file from view folder.

Love Pandey
  • 330
  • 2
  • 9
0

I faced the same issue. I tried Clean, Build and Rebuild solution, but didn't work for me.

So at last I did as below:

  1. Press Windows Key + R
  2. Type %temp%
  3. Hit Enter
  4. Delete all the files (If few file are left Undeleted, no issues)

Hope it works for you guys also.

Sadiq
  • 313
  • 2
  • 10
0

After upgrading one of my client’s applications from ASP MVC 4 to ASP MVC 5, We had designers errors related to Html Helpers and ViewBag in cshtml pages.

To fix those issues make sure you do the following: Install Asp.NET Web Helpers Library from Nuget Open the Web.config of the project and update the bindings if they haven’t been updated when installing the Nuget packages for example

<dependentAssembly>
  <assemblyIdentity name=”System.Web.Mvc” publicKeyToken=”31bf3856ad364e35″ />
  <bindingRedirect oldVersion=”0.0.0.0-5.2.2.0″ newVersion=”5.2.2.0” />
</dependentAssembly>

Find “webpages:Version” in the appsettings and update it to version 3.0.0.0. My web.config had

and I updated it to

Restart Visual Studio and rebuild. You may have to delete the bin folder for your project

Visit this: https://lajak.wordpress.com/2014/11/16/asp-mvc-5-viewbag-does-not-exist-in-the-current-context/

raniel garcia
  • 69
  • 2
  • 6
-1

After trying everything else and feeling a bit frustrated, I upgraded the .NET framework on my MVC5 app from 4.5 to 4.5.2. Somehow it fixed my problem. Hope it helps.

-3

I solved replacing in web.config

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

with

 <add key="webpages:Enabled" value="true" />