skip to Main Content

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


  1. you can findout the index of the item

     @{
    int indx=0;}
    
    @foreach (var item in l_IEnumerableModUserQuery)
                    {
                    <tr>
                        <td> 
                            @l_IEnumerableModUserQuery.IndexOf(item) 
                        </td>
                        <td>
                            <a href="#">
                                @item.Pr_FullName
                            </a>
                        </td>
                        <td>@item.Pr_Email</td>
                        <td>@item.Pr_ContactNo</td>
                    </tr>
                    }
    
    Login or Signup to reply.
  2. You could use a simple for loop to get the index:

    //use .Count if it is a List or .Count() with Linq to get the boundary.
    @for(var i = 0; i < l_IEnumerableModUserQuery.Count; i++)
    {
        <tr>
            <td> 
                @i.ToString();
            </td>
            <td>
                <a href="#">
                    @l_IEnumerableModUserQuery[i].Pr_FullName
                </a>
            </td>
            <td>@l_IEnumerableModUserQuery[i].Pr_Email</td>
            <td>@l_IEnumerableModUserQuery[i].Pr_ContactNo</td>
        </tr>
    }
    

    Thomas Levesque has a neat approach on his blog, using an extension method:

    public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
    {
        return source.Select((item, index) => (item, index));
    }
    

    Which would result in:

    @foreach (var (item, idx) in l_IEnumerableModUserQuery.WithIndex())
    {
        <tr>
            <td> 
                @idx
            </td>
            <td>
                <a href="#">
                    @item.Pr_FullName
                </a>
            </td>
            <td>@item.Pr_Email</td>
            <td>@item.Pr_ContactNo</td>
        </tr>
    }
    

    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:

    var l_IEnumerableModUserQuery = 
         someSource.Where(x => ...)
                   .Select((x, index) => new MyModel {
                         Index = index,
                         Pr_Email = xxx,
                         Pr_Contact = xxy,
                         /* ... rest of model */
                   });
    return l_IEnumerableModUserQuery;
    

    After this you could access the index like any other property in your view:

     <a href="#">
           @item.Index
     </a>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search