将视图模型数据转换为json

问题描述:

我正在尝试将视图模型数据转换为JSON,但是,我似乎无法访问@Json.Encode.我添加了System.Web.Helpers参考,但这没有用.我在做什么错了?

I'm trying to convert my view model data to JSON, however, I can't seem to access @Json.Encode. I've added the System.Web.Helpers reference, but that didn't work. What am I doing wrong?

将其放在视图顶部

@using Newtonsoft.Json

然后您可以执行此操作

@Html.Raw(@JsonConvert.SerializeObject(ViewBag.MyStuff))