mvc 4 assembly reference missing for Json.Encode
Asked Answered
M

3

30

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)) } }
Misapply answered 1/10, 2012 at 22:40 Comment(6)
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.Misapply
Looks like no such thing as Json.EncodeSettlement
There are articles all over the web that say differently (such as this one: blog.falafel.com/Blogs/basem-emara/2012/05/16/…). However, none of them seem to address just how Json.Encode is referenced!Misapply
https://mcmap.net/q/149902/-how-do-i-write-unencoded-json-to-my-view-using-razorSettlement
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?Misapply
Try this maybe: #8038395Settlement
I
71
  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

Inexpedient answered 24/11, 2012 at 12:36 Comment(4)
So I'm here again and again finding this answer helpful. TanksConfession
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 asp.net/mvc/tutorials/mvc-5/… and you can get it via Nuget UPDATE Actually this package is missing at the moment but it should reappear soon I guess.Kaftan
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?Couple
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.Breechcloth
H
6

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;
Hosanna answered 19/4, 2013 at 15:50 Comment(0)
M
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.

Misapply answered 26/10, 2012 at 14:25 Comment(2)
That's not good enough for me^^. Really dont want to move everything in to a new project.Inexpedient
I had to add a hint path: <HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.Helpers.dll</HintPath>Wolf

© 2022 - 2024 — McMap. All rights reserved.