WebMatrix - CS0103: The name 'ViewBag' does not exist in the current context

-1

Good afternoon!

I'm implementing an application in the Web Matrix. My page is in .cshtml (c # with html). I want to use the ViewBag class, but I can not find it in @using System.Web.Mvc; or elsewhere. I searched the internet but I did not quite understand the procedure. I downloaded the dll Unity.mvc3 but it also did not solve.

My .cshtml file looks like this:

@using System;
@using System.Collections;
@using System.Collections.Generic;
@using System.ComponentModel;
@using System.Linq;
@using System.Text;
@using System.Web;
@using Newtonsoft.Json;

@{

    ArrayList header = new ArrayList { "Task Name", "Hours"};
    ArrayList data1 = new ArrayList {"Work", 2};
    ArrayList data2 = new ArrayList { "Eat", 2 };
    ArrayList data3 = new ArrayList { "Sleep", 2 };
    ArrayList data = new ArrayList {header, data1, data2, data3};

    string dataStr = JsonConvert.SerializeObject(data, Formatting.None);

    ViewBag.Data = new HtmlString(dataStr);
}

<script>
    var data = JSON.parse('@ViewBag.Data');
    console.log(data);
</script>

<!DOCTYPE html>
<style>
    table {
        border: 1px #c0c0c0 solid;
        width: 100%;
    }
    table th {
        background-color: #00f;
        color: #fff;
    }
    table td {
        background-color: #f0f0f0;
        color: #0c0c0c;
    }
</style>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Teste</title>
    </head>
    <body>
    </body>
</html>

In Internet searches I heard that I should do some configuration in views/web.config , mine is empty. I tried setting the examples on the internet, but the ViewBag class was not recognized either. Below my views/web.config :

Hereisalsothedllfolder:

HereisalsotheWeb.config:

Should I install mvc3 or any dll? Thanks in advance for the help.

    
asked by anonymous 03.09.2016 / 21:12

1 answer

1

I'm going to give you an example of Views/Web.config of ASP.NET MVC5 (I'm looking for a good example of MVC4 but it's difficult, so I asked if it was possible to change the framework version) :

<?xml version="1.0" encoding="utf-8"?>

<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.2.3.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.Optimization" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler" />
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

With this, @ViewBag should be recognized in View .

I installed Visual Studio 2012 here to generate Views/Web.config for you in MVC4:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.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=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.Optimization"/>
        <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=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.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>
    
15.09.2016 / 15:08