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

前端 未结 16 1111
逝去的感伤
逝去的感伤 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 09:06

    I had the same problem and crimbo gave me the right clue, it was caused by the ./Views/Web.config file which was present but not containing the right namespaces I guess...

    I created a blank MVC5 project and imported its ./Views/Web.config into my existing project and the red waves under every ViewBag use are gone !

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

    I was having the same problem. Turned out I was missing the ./Views/Web.config file, because I created the project from an empty ASP.NET application instead of using an ASP.NET MVC template.

    For ASP.NET MVC 5, a vanilla ./Views/Web.config file contains the following:

    <?xml version="1.0"?>
    
    <!-- https://stackoverflow.com/a/19899269/178082 -->
    <configuration>
        <configSections>
            <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
                <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            </sectionGroup>
        </configSections>
    
        <system.web.webPages.razor>
            <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.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>
    
        <appSettings>
            <add key="webpages:Enabled" value="false" />
        </appSettings>
    
        <system.web>
            <httpHandlers>
                <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
            </httpHandlers>
    
            <!--
                    Enabling request validation in view pages would cause validation to occur
                    after the input has already been processed by the controller. By default
                    MVC performs request validation before a controller processes the input.
                    To change this behavior apply the ValidateInputAttribute to a
                    controller or action.
            -->
            <pages
                    validateRequest="false"
                    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
                    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
                    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <controls>
                    <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
                </controls>
            </pages>
        </system.web>
    
        <system.webServer>
            <validation validateIntegratedModeConfiguration="false" />
    
            <handlers>
                <remove name="BlockViewHandler"/>
                <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
            </handlers>
        </system.webServer>
    </configuration>
    

    Adding a ./Views/Web.config file containing this content fixed this problem for me.

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

    I updated webpages:Version under in ./Views/Web.Config folder but this setting was also present in web.config in root. Update both or remove from root web.config

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

    After trying all approaches, none of them worked for me since all I have was correct configurations. finally

    Deleting all files from "temp" and "%temp%" from system helped to resolve this issue.

    Open Run command(Windows+R) and type above strings and delete all temporary files.

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