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

前端 未结 16 1110
逝去的感伤
逝去的感伤 2020-11-28 08:24

I am trying to use ViewBag in my application, I have all of the recent dlls, the latest version of MVC 3, but yet I am still getting the Error:

\"The

相关标签:
16条回答
  • 2020-11-28 08:57

    In my case, changing the webpage:Version to the proper value resolved my issue, for me the correct value was(2.0.0.0 instead of 3.0.0.0) :

    <appSettings>
            <add key="webpages:Version" value="2.0.0.0"/>
            <add key="webpages:Enabled" value="false"/>
    
    0 讨论(0)
  • 2020-11-28 09:00

    I had this problem after changing the Application's Default namespace in the Properties dialog.

    The ./Views/Web.Config contained a reference to the old namespace

    0 讨论(0)
  • 2020-11-28 09:00

    I had already tried deleting the bin and obj file and restarting VS and had no luck.

    I've also had this issue many times and it's a pain to solve each time. Often it is due to the web.config file not having the correct version of one of the references. This means click on the reference in Visual Studio to see the version in the property tab, and then match it to the version in the web.config files.

    Another way is (if possible) upgrade to a later version of the .net framework and then deleting bin/obj files and restarting Visual Studio. I can only assume it's changing something in the

    A quick check of the diff between the csproj file doesn't actually show any major difference... But the differences it did show was (I've added (remove) to show the old line)

    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> (remove)
    <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>(remove)
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    

    In the Web.Config file (not the one in View)

    <add key="webpages:Version" value="2.0.0.0" /> (remove)
    <add key="webpages:Version" value="3.0.0.0"/>
    

    It also added (to the same web.config file) but I manually removed it

     <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
        </compilers>
      </system.codedom>
    

    Finally, in the Package Manager Console, add update-package

    Run the website locally and see any compilation errors which was fixed by my second paragraph (matching the versions of the references)

    0 讨论(0)
  • 2020-11-28 09:02

    I had a ./Views/Web.Config file but this error happened after publishing the site. Turns out the build action property on the file was set to None instead of Content. Changing this to Content allowed publishing to work correctly.

    0 讨论(0)
  • 2020-11-28 09:02

    As @Wilson Vallecilla already mentioned. Please do the below steps to delete the cache:

    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 works for me.

    Thanks!

    0 讨论(0)
  • 2020-11-28 09:03

    You need to add the MVC-specific Razor configuration to your web.config. See here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found

    Use the MVC 3 upgrade tool to automatically ensure you have the right config values.

    0 讨论(0)
提交回复
热议问题