In my CS file I'm executing the following and it works as expected.
using System.Web.Helpers;
String json = System.Web.Helpers.Json.Encode(null);
However, in my CSHTML file I'm executing the following, and here, I get an error about Json not being recognized in the context.
@{ Layout = null; }
@using TestService.ServiceReference;
@using System.Web.Helpers;
<!DOCTYPE html>
<html>
...
<script type="text/javascript">
var output3 = "! @Html.Raw(Json.Encode(ViewBag.MyArray))";
...
How can that be explained/remedied? Googling gave me nada, zero, ziltch...
Edit
I've added assemblies tag to my CONFIG file as suggested but the error I'm getting is that it's an unknown to the configuration. This is what my (root) CONFIG looks like.
<system.web>
<pilation debug="true" targetFramework="4.0" />
<assemblies>
<add assembly="System.Web.Helpers, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
...
However, I've noticed that I do have the following in the CONFIG file instead. I'm guessing it's equivalent. Is it?
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
In my CS file I'm executing the following and it works as expected.
using System.Web.Helpers;
String json = System.Web.Helpers.Json.Encode(null);
However, in my CSHTML file I'm executing the following, and here, I get an error about Json not being recognized in the context.
@{ Layout = null; }
@using TestService.ServiceReference;
@using System.Web.Helpers;
<!DOCTYPE html>
<html>
...
<script type="text/javascript">
var output3 = "! @Html.Raw(Json.Encode(ViewBag.MyArray))";
...
How can that be explained/remedied? Googling gave me nada, zero, ziltch...
Edit
I've added assemblies tag to my CONFIG file as suggested but the error I'm getting is that it's an unknown to the configuration. This is what my (root) CONFIG looks like.
<system.web>
<pilation debug="true" targetFramework="4.0" />
<assemblies>
<add assembly="System.Web.Helpers, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
...
However, I've noticed that I do have the following in the CONFIG file instead. I'm guessing it's equivalent. Is it?
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Nov 1, 2013 at 12:24
Konrad VilterstenKonrad Viltersten
39.3k84 gold badges286 silver badges509 bronze badges
10
-
@Html.Raw(System.Web.Helpers.Json.Encode(ViewBag.MyArray))
? – Mike H. Commented Nov 1, 2013 at 12:35 - Nope. Have tested that first of all (that's why I have output3, hehe). Same misbehavior. :( – Konrad Viltersten Commented Nov 1, 2013 at 12:40
-
@KonradViltersten what is the data type for
ViewBag.MyArray
? – Kami Commented Nov 4, 2013 at 11:19 - stackoverflow./questions/12682128/… – Kami Commented Nov 4, 2013 at 11:36
- @Kami It's a Dictionary but I hardly think that's the problem, since it's working on the server side but not on client (the Json class isn't found to begin with, so type errors are unlikely to be the cause). – Konrad Viltersten Commented Nov 6, 2013 at 8:34
2 Answers
Reset to default 6 +50I was able to recreate your problem and also found a solution. As to why this solution works, I cannot say, but here are the steps I followed.
1.From a fresh MVC 4 project (Basic), I created a View with the following mark up (note ReSharper 7 had no plaints)
@{
ViewBag.Title = "Index";
ViewBag.Array = new string[] {"apples", "oranges", "bananas"};
}
<script type="text/javascript">
var stuff = "! @Html.Raw(Json.Encode(ViewBag.Array))";
alert(stuff);
</script>
<h2>Index</h2>
2.Compiled and ran the application, received the following error: CS0103: The name 'Json' does not exist in the current context
.
3.Attempted to import namespace in View, added assembly to Views web.config, ensured proper reference in project. None of these had an effect.
4.The Solution Added the following lines (<assemblies>
<add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
) to my web.config in the pilation section (the primary web.config, not the Views folder one)
<system.web>
<httpRuntime targetFramework="4.5" />
<pilation debug="true" targetFramework="4.5">
<!--Add the following. Ensure pilation is not self closing,
it was on mine-->
<assemblies>
<add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</pilation>
5.Repile and here is the resulting HTML page output sans error
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<script type="text/javascript">
var stuff = "! ["apples","oranges","bananas"]";
alert(stuff);
</script>
<h2>Index</h2>
<script src="/Scripts/jquery-1.8.2.js"></script>
</body>
</html>
Make sure you're referencing System.Web.Helpers
in your project and set "copy local" to true