I am new to ASP.NET Core development. I am looking for something like a built-in way to use loop iteration numbers inside the view of ASP.NET Core.
I did some research and found solutions like creating int variable outside the loop and then increment inside the loop.
I want to index each user.
Here is my code:
@foreach (var item in l_IEnumerableModUserQuery)
{
<tr>
<td>
<!-- Here I want to add Iteration No. here-->
</td>
<td>
<a href="#">
@item.Pr_FullName
</a>
</td>
<td>@item.Pr_Email</td>
<td>@item.Pr_ContactNo</td>
</tr>
}
2
Answers
you can findout the index of the item
You could use a simple for loop to get the index:
Thomas Levesque has a neat approach on his blog, using an extension method:
Which would result in:
With an eye on the extension methods approach, you could as well amend your views model and include the index as a property in your model inside your controller / handler or whereever your model is created:
After this you could access the index like any other property in your view: