29

I am trying to call a Json.Encode(...) method in MVC 4 and I'm getting an error: The name 'Json' does not exist in the current context.

I've tried adding System.Runtime.Serialization namespace, which has Json class, but this particular instance of Json class doesn't have Encode method.

What assembly am I missing here?

Here is my exact code:

{ url: '@Url.Action("DeleteDefinitionRule")', editData: 
    { companyCode: 'LO', definitionID:@Html.Raw(Json.Encode(Model.DefinitionID)) } }
Igor Kustov
  • 3,228
  • 2
  • 34
  • 31
Scottie
  • 11,050
  • 19
  • 68
  • 109
  • That's the question. I know I'm supposed to be using something, but I don't know what. I can't find it to save my life using google. – Scottie Oct 01 '12 at 22:51
  • Looks like no such thing as Json.Encode – Lews Therin Oct 01 '12 at 22:55
  • There are articles all over the web that say differently (such as this one: http://blog.falafel.com/Blogs/basem-emara/2012/05/16/convert-a-c-object-to-javascript-in-mvc-razor). However, none of them seem to address just how Json.Encode is referenced! – Scottie Oct 01 '12 at 23:09
  • http://stackoverflow.com/a/4072787/855421 – Lews Therin Oct 01 '12 at 23:15
  • It appears that this used to be located in System.Web.Helpers, but when I run object browser on that class, it's not in there anymore. Was this moved to somewhere else? – Scottie Oct 01 '12 at 23:45
  • Try this maybe: http://stackoverflow.com/questions/8037895/where-can-i-find-system-web-helpers-system-web-webpages-and-system-web-razor – Lews Therin Oct 01 '12 at 23:48

3 Answers3

70
  1. References in solutions explorer > right click > Add reference
  2. Under Assemblies > Framework, search for "System.Web.Helpers" Double click
  3. System.Web.Helpers version 2.0.0.0 and click ok. Find the
  4. System.Web.Helpers you've just added > right click it > properties.
  5. Change "Copy Local" from false, to true

And now @Json.Encode should work.

Update

enter image description here

BjarkeCK
  • 5,694
  • 5
  • 41
  • 59
  • 1
    So I'm here again and again finding this answer helpful. Tanks – Sergey Metlov Aug 09 '13 at 19:53
  • 1
    Also if you run into this issue when using MVC 5 RC (probably the same will go for MVC 5 RTM), this assembly package has been rebranded as Microsoft.AspNet.WebHelpers as per instructions here http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2 and you can get it via Nuget UPDATE Actually this package is missing at the moment but it should reappear soon I guess. – mare Oct 16 '13 at 12:02
  • Thank you, it solved the problem. But i'm wondering why we need to set "copy always". Why the namespace can't be simply resolved with a @using System.Web.Helpers? – fabriciorissetto Jun 27 '14 at 16:39
  • This for some reason didn't work for me. I have the same version of the web.helpers dll and Razor won't find Json.Encode. – eaglei22 Dec 15 '15 at 14:44
5

To be clear, even after you add the correct reference to your project, you still have to add this line at the top of your Razor page:

@using System.Web.Helpers;
MattSlay
  • 9,115
  • 5
  • 43
  • 52
0

Apparently if you create a new empty web project, and try and move everything into Areas, it removes the Helpers library. But if you create it as an internet project, they remain. I created a new project as an internet project and everything works now.

Scottie
  • 11,050
  • 19
  • 68
  • 109
  • 3
    That's not good enough for me^^. Really dont want to move everything in to a new project. – BjarkeCK Nov 23 '12 at 14:52
  • I had to add a hint path: ..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.Helpers.dll – tofutim Feb 12 '13 at 06:55