Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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?