Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'ViewBag' does not exist in the current context - Visual Studio 2015

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.

Thanks in advance.

like image 243
Michjuh Avatar asked Mar 28 '15 15:03

Michjuh


People also ask

Is the name ViewBag does not exist in the current context?

The name 'viewbag' does not exist in the current context. The name 'HTML' does not exist in the current context. The type or namespace 'MVC' name does not exist in the namespace 'System.

Does not exist in current context asp net?

Many a times while writing a code we get this error which says, “The name does not exists in the current context”. This is because the code behind file is not able to find the control being added to your aspx file.


1 Answers

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.

like image 103
Fenton Avatar answered Oct 12 '22 17:10

Fenton