MVC3中从Json对象动态创建List类型视图

发布于 2024-12-15 15:17:00 字数 1129 浏览 0 评论 0原文

我有一个访问 WCF 服务的控制器,该服务返回一个 Json 对象(集合)。所有行的类型相同,但在不同的调用中,行结构不同(返回对象来自用户构建的sql查询,使用executeReader执行并序列化为Json 所以我事先不知道行结构。

我需要的是一种简单的方法来将此 Json 字符串传递给某个东西,该东西将为其动态生成类型列表的视图。格式等无关紧要,只是应该轻松输出为表格。

有谁知道我怎样才能做到这一点?

另一种选择可能是为匿名对象的 IEnumerable 动态生成视图(因为使用 this 我可以将 json 转换为匿名列表)


编辑

我发现一些东西几乎可以满足我的需要,除了它显示有关传递对象的元数据。

它是 preetyPrint.js,我将其集成到我的页面中,如下所示: 在我的控制器中,我将结果 json 对象设置为 ViewBag.Result,并在视图中使用以下代码:

    <script src="@Url.Content("~/Scripts/prettyprint.js")" type="text/javascript">    </script>
    <div id="resultGrid"></div>
    <script>
    var resultObject = @Html.Raw(ViewBag.Result);            
    var ppTable = prettyPrint(resultObject);
    document.getElementById('resultGrid').appendChild(ppTable);         
    </script>

有谁知道这样的脚本实际上“转储”数据而不是元数据? 谢谢。

I have a controller that access a WCF service which returns a Json object (collection). All rows are of same type, but at different calls the row stricture is different (the return object comes from a user-built sql query, executed with executeReader and serialized as Json
So I don't know the row structure upfront.

What I need is an easy way to pass this Json string to something which will generate a view of type list on the fly for it. Doesn't matter formatting, etc, just should be output easily as a table.

Does anyone knows how can I accomplish this?

Another option might be to have something that generate the view on the fly for a IEnumerable of anonymous objects (since using this I could convert the json to a list of anonymous)


EDIT

I found something that does pretty much what I need, except it display metadata about passed object.

It is preetyPrint.js, and I integrated it in my page as below:
In my controller I set the result json object to ViewBag.Result, and in the view I used this code:

    <script src="@Url.Content("~/Scripts/prettyprint.js")" type="text/javascript">    </script>
    <div id="resultGrid"></div>
    <script>
    var resultObject = @Html.Raw(ViewBag.Result);            
    var ppTable = prettyPrint(resultObject);
    document.getElementById('resultGrid').appendChild(ppTable);         
    </script>

Does anyone knows such script that actually "dump" the data instead of metadata?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-12-22 15:17:00

如果您知道行的属性,则应该创建一个要反序列化的类。然后使用 JavaScriptSerializer 类反序列化为您创建的新类的列表。然后您可以查看 WebGrid 类来输出 HTML,或者只是手动迭代视图中的属性元数据。

创建自定义类将使您能够使用元数据来控制输出的格式或其他显示属性。

如果您无法创建自定义类,则始终可以使用 Json.NET 或 JavaScriptSerializer 反序列化为字典对象列表或 ExpandoObject / Dynamic 的什么的。然后你必须手动编写一些东西来迭代我认为的键。 MVC 中的 ModelMetadataProvider 可能能够处理这些问题,让您只需迭代视图代码中的属性即可。

You should create a class to deserialize to if you know the properties of the row. Then use the JavaScriptSerializer class to deserialize to a list of your new class you created. Then you can take a look at the WebGrid class to output the HTML, or just manually iterate over the property metadata in your view.

Creating a custom class will provide you the ability to use metadata to control formatting or other display attributes of the output.

If you cannot create a custom class, you can always use Json.NET or the JavaScriptSerializer to deserialize to a list of dictionary objects or ExpandoObject / Dynamic's or something. Then you would have to manually write something to iterate the keys I think. The ModelMetadataProvider in MVC may be able to handle these allowing you to just iterate the properties in your view code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文