简体   繁体   English

HTML表比ASP.NET MVC中的Gridview更好吗?

[英]Is a HTML Table better than a Gridview in ASP.NET MVC?

I am looking into binding some data. 我正在研究绑定一些数据。 What method of output would be better. 什么输出方法会更好。 I usually use Gridviews, but I am unsure if the table would be better now. 我通常使用Gridviews,但我不确定表现在是否更好。

If all you're looking to do is present(output) a table of data, no need for a gridview at all (besides, in MVC, you most likely wouldn't want to anyway). 如果您要做的只是存在(输出)数据表,根本不需要gridview(此外,在MVC中,您很可能不会想要)。

Simply loop through your model collection and create a new table row for each item: 只需遍历模型集合并为每个项目创建一个新的表格行:

<table>
<% foreach (var item in Model) { %>
<tr><td>FieldName</td><td><%= Html.Encode(item.Field) %></tr>
<% } %>
</table>

you can format up your table however you like by applying the appropriate css class to it. 您可以通过应用适当的css类来格式化您的表格。

EDIT: a better example to show multiple fields, also showing how to display no data. 编辑:显示多个字段的更好示例,还显示如何不显示数据。

                 <% if (Model.Count() > 0)
                { %>
                    <table width="35%">
                 <thead><tr><th>Species</th><th>Length</th></tr></thead>
                  <%  foreach (var item in Model)
                    { %>
                     <tr>
                         <td><%= item.FishSpecies%></td>
                         <td align="center"><%=item.Length%></td>
                     </tr>

                 <% } %>
                    </table>
                <% }
                else
                { %>
                No fish collected.
                <%} %>

You should really steer clear of ASP.NET Web Forms controls when using the MVC framework. 在使用MVC框架时,您应该避免使用ASP.NET Web窗体控件。 They don't play nicely together 100% of the time. 他们100%的时间都不能很好地融合在一起。

The HTML helpers/ standard HTML elements are the replacement. HTML助手/标准HTML元素是替代品。 The idea being that you have more control over what you're doing that with Web Forms. 我们的想法是,您可以通过Web窗体更好地控制您正在做的事情。

There are some nice grids available: 有一些很好的网格可用:

  1. jqGrid (example of use with MVC here ) jqGrid这里与MVC一起使用的例子)
  2. And the awesome MVCContrib project. 和令人敬畏的MVCContrib项目。

That really depends on what you are outputting. 这实际上取决于你输出的内容。 I only use tables / gridview for tabular data. 我只使用表格/ gridview作为表格数据。

For other stuff i use Repeater controller or FormView. 对于其他东西,我使用Repeater控制器或FormView。

While you can partially use ASP.NET server controls in MVC applications (eg a menu will render quite well), you should expect that they won't expose the expected or rich behavior you are used to see in ASP.NET. 虽然您可以在MVC应用程序中部分使用ASP.NET服务器控件(例如,菜单将很好地呈现),但您应该期望它们不会暴露您在ASP.NET中看到的预期或丰富行为。 There's a number of reasons for that: 原因有很多:

  • They typically rely on ViewState (and ControllerState) 它们通常依赖于ViewState(和ControllerState)
  • You are required to put them inside a form tag with runat="server". 您需要将它们放在带有runat =“server”的表单标记内。 Most controls check that this is the case indeed 大多数控件都会检查确实是这种情况
  • Related to this, they rely heavily on the postback paradigm, which is not used in ASP.NET MVC 与此相关,它们严重依赖于回发范例,而ASP.NET MVC中没有使用它

As a typical example of behavior that you won't find back, there is paging and sorting ... 作为您不会找回的典型行为示例,有分页和排序......

When you delve into it, you soon find out that the helpers available in the MVC framework allow you to generate tabular data quite efficiently .... 当您深入研究它时,您很快就会发现MVC框架中可用的帮助程序允许您非常有效地生成表格数据....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM