Depending on why you need an index, it might be easier.
I did something similar and wanted an index only so that I could easily specify a list of elements, for example:
- First items in the list
- The second item in the list, etc.
Perhaps the obvious solution was to display my display template as a list item in the order list and let the browser handle the index display. I originally did this in a table and needed an index to display in the table, but using an ordered list makes it a lot easier.
So my parent view had the following:
<ol> @Html.DisplayFor(m => m.Items) </ol>
And my template wrote about the elements in the list item using divs - note that you can use floating divs or better yet show: inline-style to get a table similar to the effect
<li> <div class="class1">@Html.DisplayFor(m => m.ItemType)</div> ... </li>
And css:
.class1 { display: inline-block; width: 150px; }
Peter Kerr
source share