Asp.net mvc 3 使用MVC3 EditorFor模板交替使用HTML表行类

Asp.net mvc 3 使用MVC3 EditorFor模板交替使用HTML表行类,asp.net-mvc-3,razor,editorfor,Asp.net Mvc 3,Razor,Editorfor,我已经尽我所能在谷歌上搜索过了,但是没有用,希望这是一个简单的问题 我正在使用Html.EditorFor作为ViewModel对象中的集合 但我希望表格的交替行具有交替类(“奇数”、“偶数”)。我尝试使用下面的方法,但在编辑器模板中使用它会重置“count”值,使每个值都相同 @helper AlternativeBackgrounds(string style1, string style2) { if (ViewBag.count == null) { Vi

我已经尽我所能在谷歌上搜索过了,但是没有用,希望这是一个简单的问题

我正在使用Html.EditorFor作为ViewModel对象中的集合

但我希望表格的交替行具有交替类(“奇数”、“偶数”)。我尝试使用下面的方法,但在编辑器模板中使用它会重置“count”值,使每个值都相同

@helper AlternativeBackgrounds(string style1, string style2)
{
    if (ViewBag.count == null)
    {
        ViewBag.count = 0;
    }
    <text>class=" @(ViewBag.count % 2 == 1 ? style1 : style2)" </text>
    ViewBag.count++;
}
@helper AlternativeBackgrounds(字符串样式1、字符串样式2)
{
如果(ViewBag.count==null)
{
ViewBag.count=0;
}
class=“@(ViewBag.count%2==1?style1:style2)”
ViewBag.count++;
}
例如:

<tbody>
    <tr @AlternativeBackgrounds("odd", "even")>
        <td style="width:200px;">

这可以通过代码实现吗

我想要达到的目标:

<tbody>
     @Html.EditorFor(x => x.SomeCollection)               
</tbody>

@EditorFor(x=>x.SomeCollection)
编辑器模板

@model = SomeModel
<tr class=??>
    <td style="width:200px;">
       @Html.LabelFor(x => x.SomeProperty)   
    </td>
    <td>
       @Html.LabelFor(x => x.SomeProperty)
    </td>
    ... etc

</tr>
@model=SomeModel
@LabelFor(x=>x.SomeProperty)
@LabelFor(x=>x.SomeProperty)
... 等

谢谢大家。

你不能用javascript/jquery来代替这些,让你的代码更干净吗

你就这么做吧

$(document).ready(function() {
  $('.myTable tr:odd').addClass('oddRow');
});
如果纯粹出于样式的原因,您也可以使用CSS,使用:


您不能使用javascript/jquery来完成所有这些工作,并保持实际代码更干净吗

你就这么做吧

$(document).ready(function() {
  $('.myTable tr:odd').addClass('oddRow');
});
如果纯粹出于样式的原因,您也可以使用CSS,使用: