动态表交替背景 - MVC3

时间:2013-08-24 21:43:03

标签: css asp.net-mvc-3 html-table

我认为我有两张桌子;尝试在一个表中更改备用行的背景。在我使用的样式文件中:

 .alternateRow
{
    table-layout: auto;
    border-collapse: collapse;
    border-spacing: 0px;
    empty-cells: hide;
}

.alternateRow tr {
     background-color:#aa0000;

}

.alternateRow tr.odd  {
     background-color:#00AA00;

}

这就是我制作动态表的方法:

<table class="alternateRow">
        <% List<Question> wrongs = ViewBag.wrongs;
       for (var i = 0; i < wrongs.Count; ++i)
       { %>
        <tr>
            <td>
                <%= wrongs[i].TheQuestion %>
            </td>
            <td>
                Correct Answer
            </td>
            <td>
                <%= wrongs[i].CorrectAnswer %>
            </td>
            <td>
                <%= wrongs[i].Explanations %>
            </td>
        </tr>
        <% } %>

我没有工作,两行都有相同的颜色。知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

以下是您可以做的事情:

<% List<Question> wrongs = ViewBag.wrongs;
for (var i = 0; i < wrongs.Count; ++i)
{ 
    var rowClass = ((i % 2) == 0 ? "" : "odd"); // or whatever your alternate row class is
%>
    <tr class="<%= rowClass %>">
        <td>
            <%= wrongs[i].TheQuestion %>
        </td>
        <td>
            Correct Answer
        </td>
        <td>
            <%= wrongs[i].CorrectAnswer %>
        </td>
        <td>
            <%= wrongs[i].Explanations %>
        </td>
    </tr>
<% } %>