ASP.NET MVC:在网格中显示 JSON,其中 JSON 数据仅在运行时已知
我花了很长时间试图解决这个问题,但到目前为止还没有成功。基本上,将有一个通过 jQuery GET 或类似方式提供的 JSON 数据集,其列数和列名有所不同。数据类型会有所不同。一次可能是姓名=鲍勃,年龄=50(等等),而下一次可能是姓名、地址和职业。
我想做的就是将其呈现在 MVC 视图的网格中,理想情况下它提供一些分页/排序等。列名称应从 JSON 中的标签派生。如果需要的话,我很乐意为这样的组件付费。
就是这样!如果我失败了,我想我只需要渲染 HTML 并且不需要其他功能。
谢谢
。实际数据将来自匿名集合,由控制器中的 LINQ 查询生成。如果有人知道如何绑定到这种类型将是完美的解决方案(按需分页会很棒)但还没有运气。迄今为止,花了 2 天的时间将 Telerik MVC 网格绑定到匿名类型或其任何转换(动态、表格等)尚未成功。我添加了 telerik MVC 和 Jqgrid 作为标签,以防有专家监视那些知道答案的部分。
I've spent a long time trying to work this out, but no success thus far. Basically, there will be a JSON dataset coming via a jQuery GET or similar, which will differ in the number of columns and column names. Datatypes will vary. One time it may be Name=Bob, Age=50 (etc) while next time it may be name, address, and occupation.
All I'd like to do is present that in a grid in my MVC view, which ideally provides some paging/sorting etc. Column names should be derived from the labels in the JSON. I'm happy to pay for such a component if needed.
That's it! If I fail I guess I'll just have to render HTML and do without the other features.
Thanks
PS. The actual data will come from anonymous collections, generated by LINQ queries in the controller. Binding to such a type would be the perfect solution if anyone knows how (on demand paging would be great) but no luck yet. 2 Days of getting the Telerik MVC grid to bind to an anonymous type or any conversion of it (dynamics, tables, etc) has been unsuccessful so far. I've added telerik MVC and Jqgrid as tags in case there are gurus monitoring those sections who know the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要查看 DataTables jQuery 插件。它内置了分页、排序和许多其他很酷的功能。大多数示例显示在页面上呈现整个表格,然后它接管分页等。但是,它也支持使用服务器端绑定,这里是 ASP.NET MVC 中的绑定的一个很好的示例站点 - jQuery DataTables 和 ASP.NET MVC 集成。
我已经使用这个插件大约一个月了,到目前为止我对它非常满意。
You might want to look at the DataTables jQuery plug-in. It has built in paging, sorting and a lot of other cool features. Most of the examples show rendering the entire table on the page and then it takes over with the paging, etc. It does however, also support the use of server side binding and here is a good example of that binding within a ASP.NET MVC site - jQuery DataTables and ASP.NET MVC Integration.
I have been using this plug-in for about a month now and have been very happy with it so far.
我只使用 jqGrid 而没有使用 Telerik MVC,所以我只能在 jqGrid 实现方面为您提供帮助。
有一些不同的方法可以实现可变数量的列和其他 jqGrid 参数。
最简单的方法是使用 GridUnload 方法(请参阅旧演示来自答案),重新创建了基础
< /code> 网格元素,之后您可以创建新 jqGrid。因此,您可以只进行
$.ajax
调用,该调用从您需要的服务器colModel
加载,并在success
处理程序内部加载$.ajax 你调用 GridUnload 然后创建 jqGrid。如果您的列数可变且格式已知,您可以使用另一个描述的aproatch 此处 和 此处(请参阅演示 和 演示)。只需要一些隐藏列,并在
loadComplete
内部使其可见并覆盖列名称。I use only jqGrid and no Telerik MVC, so I can help you only in he jqGrid implementation.
There are some different approaches how you can implement variable number of columns and ather jqGrid parameters.
The easiest way is to use GridUnload method (see the old demo from the answer) which recreate the basis
<table>
element of the grid after which you can create new jqGrid. So you can just has an$.ajax
call which load from the servercolModel
which you need and inside ofsuccess
handler of the$.ajax
you call GridUnload and then create jqGrid.If your has variable number of columns with the knows format you can use another aproatch described here and here (see the demo and the demo). One need just have some hidden columns, and inside of
loadComplete
to make it visible and overwrite the column names.