I have an mvc page with a displaytemplate. How do I get the index of the current item being rendered in the displaytemplate. it produces correct bindable results in the name attributes.
<input name="xxx[0].FirstName"/>
<input name="xxx[1].FirstName"/>
I want that index value in the display template. Is it in the ViewContext somewhere?
@* page.cshtml @ @model ...@ property Contacts is IEnumerable *@
<table id="contacts" class="editable">
<thead>
<tr><th>Name</th><th>Surname</th><th>Contact Details</th></tr>
</thead>
<tbody>
@Html.DisplayFor(x => x.Contacts)
</tbody>
in the display template we have
@* contact.cshtml *@
@model ...@* This is the T of the IEnumerable<T> *@
<tr>
@* I NEED THE INDEX OF THE CURRENT ITERATION HERE *@
<td>@Html.DisplayFor(x => x.FirstName)</td>
</tr>
Extending @DarinDimitrov's answer, I wanted to submit a complete solution:
Extention:
Parent View Markup:
Template Markup:
Depending on why you need the index, there may be an easier way.
I was doing something similar and wanted the index just so I could easily number the list of items such as:
The maybe obvious solution was to render my display template as a List Item within an Order List, and let the browser handle the display of the index. I was originally doing this in a table and needed the index to display in the table, but using a ordered list makes it much easier.
So my parent view had the following:
And my template wrote on the items in an list item using divs - note you could use floating divs, or better yet display: inline-style to get a table like effect
And the css:
I am afraid there is no easy way to get the index. It's buried inside the internal
System.Web.Mvc.Html.DefaultDisplayTemplates.CollectionTemplate
method and not exposed. What you could use is the field prefix:Another possibility is replace
@Html.DisplayFor(x => x.Contacts)
with:and then inside the template
@ViewBag.Index
should give you the current index but I must admit it's kinda ugly.