Webmatrix - CS0103: The name 'Viewbag' does not exist in the current context

Asked

Viewed 1,783 times

-1

Good afternoon!

I’m implementing a Web Matrix application. My page is in . cshtml (c# with html). I want to use the Viewbag Class, but I can’t find it in @using System.Web.Mvc; nor elsewhere. I searched the internet but did not understand well the procedure. I downloaded the dll Unity.mvc3 but it also did not resolve.

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 on views/web.config, mine is empty. I tried to put the parameterization of the examples on the internet, but the Viewbag class was also not recognized. Below my views/web.config:

inserir a descrição da imagem aqui

Here is also the dll folder:

inserir a descrição da imagem aqui

Here is also the Web.config:

inserir a descrição da imagem aqui

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

  • 1

    This way of using ViewBag is terrible. What version of ASP.NET MVC?

  • My version is 4.0_3.0.0.0. I was introduced to this tool (Webmatrix) and I met cshtml through it. I’m still learning how to deal with it. In fact the ViewBag nor is being recognized, this is being treated as text.

  • Should I replace this version of MVC in the directory? C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35

  • 1

    I don’t think that’s a good idea. If I were you, would install MVC4 using an installer.

  • Thank you Gypsy! I’ll do what you recommended! Then I’ll tell you what happened.

  • Even installing MVC4 at the address C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_4.0.0.0__31bf3856ad364e35 the Viewbag remains unknown.

  • Question: where exactly is this accommodation? Apparently it is in Azure, right?

  • He’s on my machine locally, and I’m using IIS Express as a server.

  • Is there a specific need for the use of MVC4? It is already well outdated from the stable version today, which is MVC5. I don’t even know if it’s worth setting up MVC4.

  • It’s because I want Viewbag to be known on the page .cshtml . There in the file it was being seen as a variable and not as an object.

  • My question is another. You could not use MVC5 instead of MVC4?

  • I can, yes, no problem.

Show 7 more comments

1 answer

1


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

<?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>

Thereby, @ViewBag must be recognised in View.


I installed a Visual Studio 2012 here to generate a Views/Web.config for you at 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>
  • Thanks for the support, Gypsy! I’ll do the procedures you gave me and then I’ll tell you what happened!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.