Asp.net mvc 3 如何在没有javascript的情况下为绑定到模型列表的DisplayFor/EditorFor替换行样式

Asp.net mvc 3 如何在没有javascript的情况下为绑定到模型列表的DisplayFor/EditorFor替换行样式,asp.net-mvc-3,Asp.net Mvc 3,如何将当前元素的索引传递给DisplayFor/EditorFor视图,以便决定行是否应显示备用样式 我的主要观点如下: <table> @Html.EditorFor(model => model.MyListOfItems) </table> @EditorFor(model=>model.MyListOfItems) 用于EditorFor的视图如下所示: @Html.HiddenFor(model => model.IdOfTheItem) &l

如何将当前元素的索引传递给DisplayFor/EditorFor视图,以便决定行是否应显示备用样式

我的主要观点如下:

<table>
@Html.EditorFor(model => model.MyListOfItems)
</table>

@EditorFor(model=>model.MyListOfItems)
用于EditorFor的视图如下所示:

@Html.HiddenFor(model => model.IdOfTheItem)
<tr class="shouldBeChangedDependingOnRowEvenOrNot">
<td>@Html.CheckBoxFor(model => model.MarkForBatchEdit)</td>
<td>@Html.DisplayFor(model => model.NameOfThisItem)</td>
<td>@Html.DisplayFor(model => model.StateOfThisItem)</td>
</tr>
@Html.HiddenFor(model=>model.IdOfTheItem)
@Html.CheckBoxFor(model=>model.MarkForBatchEdit)
@DisplayFor(model=>model.nameofthitem)
@DisplayFor(model=>model.stateofthistem)
我知道类似的问题和建议的解决方案:

但我不能把它们应用到这个案子上


有什么建议吗?

编辑器for重载以获取参数,因此您可以在中传递索引,这是键/值对的集合

@Html.EditorFor( model => model.MyListOfItems , new { CurrentIndex = SomeNumber } )
在您的视图中,您可以使用
ViewData[“CurrentIndex”]
获取该值

另外,与其传递元素索引,为什么不在控制器中进行计算,并传递
ViewData
中是否有偶数行或奇数行

bool isEvenRow = ((CurrentElementIndex % 2) == 0);
ViewData["isEvenRow"] = isEvenRow;

然后,您将根据值是真还是假在视图中切换CSS

事实上,我刚刚发现了这个问题(这是一个非常类似的问题):我对所有东西都使用模型:因此我会将属性IsEventRow添加到MyItem中,但我仍然不确定从何处获得这个CurrentElementIndex,而不必对所有元素进行额外的foreach循环。哦,实际上,我可以在使用LINQ的控制器中使用索引。实际上,我在后面用一个属性扩展了我的视图,该属性在查询列表时被填充。谢谢你的帮助。