2

I have the following _ViewStart.cshtml:

@{
    ViewBag.Version = 1;
    ViewBag.Debug = false;
}

However, I am getting the following errors:

[ERROR] The name 'ViewBag' does not exist in the current context (@ line 2)

[ERROR] The name 'ViewBag' does not exist in the current context (@ line 3)

[WARNING] ASP.NET runtime error: Could not load file or assembly 'System.Web.Helpers' or one of its dependencies.

I have installed the latest version of ASP.NET MVC (5.2.3) via NuGet.

Project references

As you can see, System.Web.Helpers is referenced.

Also, I have the following page namespaces in my Views/Web.config:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
         <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>

Why can't I locate the ViewBag property and why does that warning shows up?

UPDATE

Using ViewContext.ViewBag instead of just ViewBag seems to compile and work. But why is this happening?

Community
  • 1
  • 1
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • This might be a stupid comment, but have you tried a clean/rebuild? And are the DLLs in your bin folder for the dependencies? – Luke Oct 16 '15 at 14:22
  • @Coulton Yep, like six times already. Not a stupid comment at all, I checked other questions and rebuilding seemed to solve the issue. It didn't work for me, unfortunately – Matias Cicero Oct 16 '15 at 14:23
  • Have you tried these? http://stackoverflow.com/questions/4960148/the-name-viewbag-does-not-exist-in-the-current-context – Duston Oct 16 '15 at 14:23
  • When I've had issues like this, I'm sure that doing an update on Manage Nuget Dependencies fixed this for me. I don't know if that's an option to you just to test. – Luke Oct 16 '15 at 14:24
  • @Duston Yes, it seems my Web.config contains all it needs to have, but the problem persists – Matias Cicero Oct 16 '15 at 14:26
  • @Coulton Where is this `Manager Nuget Dependencies` button? If you mean `Manage Nuget Packages`, then I already tried. All my packages are up-to-date. – Matias Cicero Oct 16 '15 at 14:27
  • Yes that's the one I mean :). If they're up to date then it's not really an option. It's like you need to refresh all of your NuGet packages. – Luke Oct 16 '15 at 14:28
  • Perhaps running `Update-Package –reinstall ` on the related packages could help? or deleting the DLLs from the packages folder and rebuilding your app? Short of that I'm at a loss, I'll have to leave it to others. – Luke Oct 16 '15 at 14:29
  • @Coulton Ran that command with `` equals to `Microsoft.AspNet.Mvc` but still no luck :( – Matias Cicero Oct 16 '15 at 14:33
  • Check the properties of your application for the bin location and confirm that the DLLs required are in that folder after the build. – Luke Oct 16 '15 at 14:36
  • @Coulton `System.Web.Mvc.dll` and `System.Web.Helpers.dll` are in that folder along with ~5 more DLLs that must be the dependencies – Matias Cicero Oct 16 '15 at 14:38

2 Answers2

1

Which versions of the assemblies do you currently have?

According to https://www.nuget.org/packages/Microsoft.AspNet.Mvc/5.2.3 Microsoft ASP.NET MVC 5.2.3 has the following dependencies:

    Microsoft.AspNet.WebPages (≥ 3.2.3 && < 3.3.0)
    Microsoft.AspNet.Razor (≥ 3.2.3 && < 3.3.0)

And according to https://www.nuget.org/packages/Microsoft.AspNet.WebHelpers/ ASP.NET Web Helpers Library 3.2.3 has the following dependencies:

    Microsoft.AspNet.WebPages (≥ 3.2.3 && < 3.3.0)
    Microsoft.AspNet.WebPages.WebData (≥ 3.2.3 && < 3.3.0)

Perhaps nuget did not pull all the required versions.

DanielC
  • 357
  • 1
  • 4
  • 10
  • https://www.nuget.org/packages/Microsoft.AspNet.WebHelpers/. I don't need that package, that is just a helper library for adding new features to a website, it has nothing to do with `System.Web.Helpers` – Matias Cicero Oct 16 '15 at 14:45
  • You are right, that assembly is not related. Regarding System.Web.Helpers I found the following question: http://stackoverflow.com/questions/8037895/where-can-i-find-system-web-helpers-system-web-webpages-and-system-web-razor perhaps you need to change target framework from '.NET framework 4 client profile' to '.NET framework 4'? – DanielC Oct 16 '15 at 14:58
-1
  1. Remove MVC assemblies
  2. Get right version of MVC assemblies like C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 1.0\Assemblies\System.Web.Mvc.dll.
jalil
  • 85
  • 10