0

I'm trying to apply some odd/even style to rows of data rendered by a Razor editor template in ASP.NET MVC 3.

Here is some basic code of my possible view:

@model List<MyProject.Whatever>

@Html.EditorFor(Model)

And here the possible editor template:

@model MyProject.Whatever

<div class="@( AnIInAnEvenOrOddRow ? "even" : "odd" )">
  @Model.SomeData @Html.TextBoxFor(m => m.SomeTextToType)
</div>

Have anybody an idea how I can detect the even/odd situation from inside the editor template?

Cheers...

fredlegrain
  • 584
  • 1
  • 6
  • 20

1 Answers1

1

I always interpret Rows of data == @WebGrid witch has already that feature included and you can easily implement paging and sorting as well, why not using it?

if you still want to do everything manually, you can use something like

@{
    int iRow = 0;
}

@forach( var item in Model )
{
    iRow++;
    <div class="@( iRow % 2 == 0 ? "even" : "odd" )">
        @Model.SomeData @Html.TextBoxFor(m => m.SomeTextToType)
    </div>
}

Updated

the WebGrid helper just like it's cousin gridview in webforms is totally customizable, if you did webforms before, you know you could simple add a templatecolumn and append whatever controls you like, the WebGrid Helper is the same, for example as as stated in this fine article:

@grid.GetHtml(        
    columns: grid.Columns(
        grid.Column(
             "Title", 
             "Movie", 
             @<text><div style='width: 10em'>@item.Title</div></text>), ...
balexandre
  • 73,608
  • 45
  • 233
  • 342