Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# ASP.NET MVC Basics Adding a List Page Finishing the List View

Davis Mercier
Davis Mercier
7,805 Points

CS0103: The name `Viewbag' does not exist in the current context

When I try to run the website after creating the Index view the following error occurs

System.Web.Compilation.CompilationException CS0103: The name `Viewbag' does not exist in the current context

Description: Error compiling a resource required to service this request. Review your source file and modify it to fix this error.

Details: CS0103: The name `Viewbag' does not exist in the current context Error origin: Compiler Error source file: /Users/davismercier/GitHub/ComicBookGallery/src/ComicBookGallery/ComicBookGallery/Views/ComicBooks/Index.cshtml

Exception stack trace: at System.Web.Compilation.AssemblyBuilder.BuildAssembly (System.Web.VirtualPath virtualPath, System.CodeDom.Compiler.CompilerParameters options) [0x00365] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System.Web/System.Web.Compilation/AssemblyBuilder.cs:853 at System.Web.Compilation.AssemblyBuilder.BuildAssembly (System.Web.VirtualPath virtualPath) [0x00000] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System.Web/System.Web.Compilation/AssemblyBuilder.cs:731 at System.Web.Compilation.BuildManager.GenerateAssembly (System.Web.Compilation.AssemblyBuilder abuilder, System.Web.Compilation.BuildProviderGroup group, System.Web.VirtualPath vp, System.Boolean debug) [0x00264] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs:826 at System.Web.Compilation.BuildManager.BuildInner (System.Web.VirtualPath vp, System.Boolean debug) [0x0011c] in /private/tmp/source-mono-4.8.0/bockbuild-mono-4.8.0-branch/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs:451

Full code for index.cshtml is below:

@model ComicBookGallery.Models.ComicBook[]

@{ Viewbag.Title = "Comic Books"; }


<div class="row"> @foreach (var comicBook in Model) { <div class="col-md-3"> <h4>@comicBook.DisplayText</h4> <img src="/Images/@comicBook.CoverImageFileName" alt="@comicBook.DisplayText" class="img-responsive" /> </div> } </div>

I don't understand why Viewbag does not exist in the current context. Any help would be appreciated

1 Answer

James Churchill
James Churchill
Treehouse Teacher


The name of the property is actually "ViewBag" not "Viewbag". Remember, C# is case-sensitive!

Thanks ~James

Hello James, I actually have the same issue and I am using the proper case. Any suggestions?