输出的Json剃刀视图引擎 [英] Outputting Json with the Razor view engine

查看:130
本文介绍了输出的Json剃刀视图引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典<字符串,字符串> 我的视图模型的一部分​​。我正在试图做的是循环该对象和输出为JSON对象。我的这个原因是这样我就可以定位我的客户端脚本正确的文件。

I've got a dictionary<string,string> as part of my view model. What I'm trying to do is cycle this object and output it as a json object. My reason for this is so I can localise my client script files correctly.

这样做的输出需要看起来像

The output for this needs to look something like

var clientStrings = {"test":"yay","goodBye":"Nah"};

任何想法如何正确地做到这一点。

Any ideas how to achieve this correctly.

先谢谢了。

推荐答案

考虑到你对MVC 3,你将有机会获得的JavaScriptSerializer。你应该能够做到以下几点:

Considering you are on mvc 3 you'll have access to JavaScriptSerializer. You should be able to do the following:

JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize((object)yourDictionary);

这将系列化你的字典,以JSON。您可能要发送到的ViewData渲染视图之前做这个控制器。

This will serialize your dictionary to json. You may want to do this in the controller before sending the ViewData to the view to render.

这篇关于输出的Json剃刀视图引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆