mvc 4 assembly reference missing for Json.Encode

最后都变了- 提交于 2019-12-17 07:28:54

问题


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)) } }

回答1:


  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




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/12682128/mvc-4-assembly-reference-missing-for-json-encode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!