使用 ASP.NET WebForms 时 ListView control为了在 HTML 表中显示数据,我使用以下技术对表行进行“ strip 化”:
<ItemTemplate>
<tr class="<%# Container.DisplayIndex % 2 == 0 ? "" : "alternate" %>">
<!-- table cells in here -->
</tr>
</ItemTemplate>
使用以下 CSS:
tr.alternate
{
background-color: #EFF5FB;
}
我刚刚浏览了 ASP.NET MVC Movie Database Application tutorial并了解到在 MVC 领域表行可以(必须是?)构造如下:
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.Title) %>
</td>
<!-- and so on for the rest of the table cells... -->
</tr>
<% } %>
我可以在这段代码中添加什么来对表格的行进行 strip 化?
注意:我知道这可以是done using jQuery ,我想知道是否可以通过其他方式完成。
编辑
如果您认为 jQuery(或同等内容)是最好或最合适的帖子,我很想知道为什么。
最佳答案
另一个不涉及 lambda 且比您正在工作的更干净的选项可能是这个......
<% int i=0; foreach (var item in Model) { %>
<tr class="<%= i++ % 2 == 0 ? "alternate" : "" %>">
<td>
<%= Html.Encode(item.Title) %>
</td>
<!-- and so on for the rest of the table cells... -->
</tr>
<% } %>
关于jquery - ASP.NET MVC 中的条纹表行(不使用 jQuery 或等效项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/756310/