Json.Encode 缺少 mvc 4 程序集参考
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12682128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mvc 4 assembly reference missing for Json.Encode
提问by Scottie
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.
我试图Json.Encode(...)在 MVC 4 中调用一个方法,但出现错误:The name 'Json' does not exist in the current context.
I've tried adding System.Runtime.Serializationnamespace, which has Jsonclass, but this particular instance of Jsonclass doesn't have Encodemethod.
我试过添加System.Runtime.Serialization命名空间,它有Json类,但这个特定的Json类实例没有Encode方法。
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)) } }
回答by BjarkeCK
- References in solutions explorer > right click > Add reference
- Under Assemblies > Framework, search for "System.Web.Helpers" Double click
- System.Web.Helpers version 2.0.0.0 and click ok. Find the
- System.Web.Helpers you've just added > right click it > properties.
- Change "Copy Local" from false, to true
- 解决方案资源管理器中的参考 > 右键单击 > 添加参考
- 在程序集 > 框架下,搜索“System.Web.Helpers”双击
- System.Web.Helpers 版本 2.0.0.0 并单击确定。找出
- 您刚刚添加的 System.Web.Helpers > 右键单击它 > 属性。
- 将“复制本地”从 false 更改为 true
And now @Json.Encode should work.
现在@Json.Encode 应该可以工作了。
Update
更新


回答by MattSlay
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:
需要明确的是,即使您为项目添加了正确的引用,您仍然需要在 Razor 页面的顶部添加以下行:
@using System.Web.Helpers;
回答by Scottie
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.
显然,如果您创建一个新的空 Web 项目,并尝试将所有内容移动到 Areas 中,它会删除 Helpers 库。但是,如果您将其创建为 Internet 项目,它们就会保留下来。我创建了一个新项目作为互联网项目,现在一切正常。

