我如何显示asp.net MVC 2的DataTable? [英] How do I display a datatable in asp.net mvc 2?

查看:202
本文介绍了我如何显示asp.net MVC 2的DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个动态生成的SQL语句,并把结果在一个DataTable。然后,我需要在一个局部视图来显示这些结果为表。我通常会创建一个列表<>对象,并强烈它输入到网页,但在这种情况下,我不知道最终的SQL会是怎样,因为它是由用户在运行时建

I am running a dynamically built SQL statement and putting the results in a datatable. I then need to display these results as a table in a partial view. I would normally create a List<> object and strongly type it to the page, but in this case I do not know what the final sql will be, since it is built by the user at run time.

所以,我怎么能显示一个DataTable中的数据时,我不知道它是什么?也有没有更好的办法做到这一点比一个DataTable?

So, how can I display the data in a datatable when I do not know what is in it? Also is there a better way to do this than in a datatable?

感谢

推荐答案

当你在显示表格式的数据,你应该使用HTML表格来完成这项工作。 Dismissile的回答只显示该表中的一列。更通用的答案可以在以下SO问题为:

As you are displaying tablular data, you should use an html table to do the job. Dismissile's answer only displays one column of the table. The more generic answer can be found in the following SO question:

<一个href=\"http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc\">http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc

这是关于你的是,使用强类型为DataTable中的模型位:

The bit that concerns you is, with the model strongly typed as a DataTable:

<table border="1">
    <thead>
        <tr>
            <%foreach (System.Data.DataColumn col in Model.Columns) { %>
                <th><%: col.Caption %></th>
            <%} %>
        </tr>
    </thead>
    <tbody>
    <% foreach(System.Data.DataRow row in Model.Rows) { %>
        <tr>
            <% foreach (var cell in row.ItemArray) {%>
                <td><%: cell.ToString() %></td>
            <%} %>
        </tr>
    <%} %>         
    </tbody>
</table>

修改

编辑,以EN code中的确定时代的内容。由于使用MVC 2(按标签),然后Html.En code不是必需的,只是在&lt;%:表示法是MVC 2可用

Edited to encode the content of the datable. As using mvc 2 (according to the tag), then Html.Encode not required, just the <%: notation that is available in mvc 2.

这篇关于我如何显示asp.net MVC 2的DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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