Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'ViewBag' does not exist in the current context

I have a Windows 2003 R2 server. I have to run a .NET MVC3 website. I already installed all the Runtimes (1,2 and 4), i have installed all the MVCs frameworks (3 and 4) but this error doesn't go away.

I have the web.config that is supposed to be in the Views in place. I have followed all and each of the solutions to this problem in and out stackoverflow. I have also deployed in my bin folder all the dll by using Add Deployable Dependencies (right click on mvc project).

At this point i don't know what else to do. Ran out of ideas or solutions online.

I already uninstalled and reinstalled all the runtimes, frameworks, etc. In my local computer it runs just fine (windows 7) but when i deploy, i get following error.

Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context

Source Error:

Line 1: @{

Line 2: ViewBag.Title = "Home Page";

Line 3: }

Line 4: <h2>@ViewBag.Message</h2>

Source File: d:\FamilyDermMVC\Views\Home\Index.cshtml Line: 2

like image 723
Pepito Fernandez Avatar asked Mar 21 '13 14:03

Pepito Fernandez


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.


2 Answers

Sounds like you're missing the following in the Web.Config in the views folder:

/Views/Web.Config

<?xml version="1.0"?>  <configuration>    <system.web.webPages.razor>     <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />        <pages pageBaseType="System.Web.Mvc.WebViewPage"> // <-- this line and contents are important         <namespaces>           <add namespace="System.Web.Mvc" />           <add namespace="System.Web.Mvc.Ajax" />           <add namespace="System.Web.Mvc.Html" />           <add namespace="System.Web.Routing" />         </namespaces>       </pages>    </system.web.webPages.razor> 

Views typically derive from System.Web.Mvc.WebViewPage which is configured in the web.config. If you are not deploying the DLL with the application, the base class is in the following DLL installed in:

Assembly System.Web.Mvc.dll, v4.0.30319

c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

Update 1

If you are manually upgrade from MVC X to MVC Y and this requires changing your target .Net Framework (say 4.5 to 4.6) that if you have old references (point to MVC 5 instead of 6) that obviously the older files cannot be used in conjunction with newer files (e.g. MVC 5 DLLs can't be used against System.Web in 4.6).

like image 199
Erik Philips Avatar answered Sep 21 '22 07:09

Erik Philips


I see this was posted awhile back, but thought I would add my experience with this issue anyway.

Upon creating a new MVC asp.net project I was getting this same error right off the bat. I simply clicked BUILD -> Clean Solution, then Build Solution and it fixed the problem for me. Still not quite sure why the error came about in the first place though as it errored out right after it first generated.

like image 28
Fütemire Avatar answered Sep 18 '22 07:09

Fütemire