The name 'ViewBag' does not exist in the current context
Asked Answered
C

10

42

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

Catlaina answered 21/3, 2013 at 14:54 Comment(2)
It would be really helpful to include your web.config and views\web.config (remove any sensitive data).Mcmillon
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.Cleek
C
1

Ok. I fixed it. I don't know exactly what fixed it but i guess this whole "9 miles" fixes it.

I removed (Remove Programs) all MVC extensions, tools and whatever you find there.

Then reinstalled, without removing, .net runtime. 2 and 4.

Then installed mvc3 and mvc4 back. I went to asp.net for the latest versions.

Then i did the same thing in my laptop.

NOTE: You will need Power Shell 2.0 in order to install MVC4 in Windows2003 server. You need to find the specific download for that server.

Once i did that, recycled app pool and restarted server.

BANG... page loaded.

I hope this helps other folks in the future.

Thank you for your answers thou.

PS: My feeling is that i didn't have the same version (or releases) of mvc3 (or mvc4) in my laptop and server respectively.

Catlaina answered 21/3, 2013 at 21:34 Comment(1)
Don't you see that I explained the whole process?Catlaina
M
78

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).

Mcmillon answered 21/3, 2013 at 16:45 Comment(3)
That part was OK. Read my own answer to this. Thanks thou.Catlaina
For me... my /Views/Web.config was marked "none" instead of "content" (for the Build Action). I changed it, re-deployed and it worked fine.Beneficence
This was it for me. My deployment process skips "web.config" and the first deployment of a new site obviously didn't copy this one in the views folder. Thanks!Westminster
H
6

I had the same type of problem (html helper code not being recognised).

I'm not sure how it started but i solved it by replacing the following:

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

with

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
</appSettings>

in the views web.config file.

Found my solution here

Haematoxylon answered 30/11, 2015 at 1:14 Comment(0)
P
5

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.

Pampas answered 3/12, 2013 at 23:9 Comment(1)
Came here to say this as well. Not entirely sure what is causing this to happen within my VS.Fishback
M
3

Sometimes closing the view, Rebuild, Open the view helps.

Mauramauralia answered 14/10, 2016 at 16:16 Comment(1)
Same. I literally just made a copy of my solution folder, re-opened the copy and got all these errors. Didn't go away till I closed all files, did a Clean and Rebuild.Salangi
A
2

I had this issue regardless of having all the correct configuration done in the web.config file.

Found 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).

Please follow below path to discover the files:

C:\Users\your.name.here\AppData\Local\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 closed my project, deleted the files on that path and reopened my project, cleaned the solution and built it again and the problem was solved

Deleting your Temporary ASP.NET Files also helps. C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files.

This way you can fix all the errors related to intelligent in the view (Razor MVC) Try this out and let me know this works for me.

Thanks!

Agc answered 12/2, 2019 at 10:29 Comment(0)
C
1

Ok. I fixed it. I don't know exactly what fixed it but i guess this whole "9 miles" fixes it.

I removed (Remove Programs) all MVC extensions, tools and whatever you find there.

Then reinstalled, without removing, .net runtime. 2 and 4.

Then installed mvc3 and mvc4 back. I went to asp.net for the latest versions.

Then i did the same thing in my laptop.

NOTE: You will need Power Shell 2.0 in order to install MVC4 in Windows2003 server. You need to find the specific download for that server.

Once i did that, recycled app pool and restarted server.

BANG... page loaded.

I hope this helps other folks in the future.

Thank you for your answers thou.

PS: My feeling is that i didn't have the same version (or releases) of mvc3 (or mvc4) in my laptop and server respectively.

Catlaina answered 21/3, 2013 at 21:34 Comment(1)
Don't you see that I explained the whole process?Catlaina
C
1

It seems there are many solutions and the success rate of each is mixed.

For me, changing the version number on this line of the Views/web.config file worked:

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

I changed 4.0.0.0 to 5.0.0.0, saved, closed the solution, re-opened, then voila! The annoying red squiggles were gones and the intellisense returned

Capable answered 12/4, 2016 at 16:11 Comment(0)
H
1

Resharper is probably causing it

Go to Visual Studio/Tools/Options/Resharper Then click Suspend Now / then Resume Resharper and your day will no longer be gray but probably your razor window will have more gray in it!

Hydrochloride answered 19/8, 2016 at 2:4 Comment(0)
G
0

In my case i got this problem because i was trying to run an MVC 3 project into MVC 2 enabled platform.

then i installed MVC 3 on my system an problem gone away

Glorify answered 4/3, 2014 at 16:46 Comment(0)
S
0

installing ASP.NET MVC 5 support for Visual Studio 2012 from http://www.asp.net/mvc/mvc5 solves the issue for me.

Sharp answered 12/1, 2015 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.