Getting index value on razor foreach

前端 未结 10 743

I\'m iterating a List in a razor foreach loop in my view which renders a partial. In the partial I\'m rendering a single record for which I want to hav

相关标签:
10条回答
  • 2020-12-08 10:11

    In case you want to count the references from your model( ie: Client has Address as reference so you wanna count how many address would exists for a client) in a foreach loop at your view such as:

     @foreach (var item in Model)
                            {
                                <tr>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.DtCadastro)
                                    </td>
    
                                    <td style="width:50%">
                                        @Html.DisplayFor(modelItem => item.DsLembrete)
                                    </td>
                                    <td>
                                        @Html.DisplayFor(modelItem => item.DtLembrete)
                                    </td>
                                    <td>
                                        @{ 
                                            var contador = item.LembreteEnvolvido.Where(w => w.IdLembrete == item.IdLembrete).Count();
                                        }
                                        <button class="btn-link associado" data-id="@item.IdLembrete" data-path="/LembreteEnvolvido/Index/@item.IdLembrete"><i class="fas fa-search"></i> @contador</button>
                                        <button class="btn-link associar" data-id="@item.IdLembrete" data-path="/LembreteEnvolvido/Create/@item.IdLembrete"><i class="fas fa-plus"></i></button>
                                    </td>
                                    <td class="text-right">
                                        <button class="btn-link delete" data-id="@item.IdLembrete" data-path="/Lembretes/Delete/@item.IdLembrete">Excluir</button>
                                    </td>
                                </tr>
                            }
    

    do as coded:

    @{ var contador = item.LembreteEnvolvido.Where(w => w.IdLembrete == item.IdLembrete).Count();}
    

    and use it like this:

    <button class="btn-link associado" data-id="@item.IdLembrete" data-path="/LembreteEnvolvido/Index/@item.IdLembrete"><i class="fas fa-search"></i> @contador</button>
    

    ps: don't forget to add INCLUDE to that reference at you DbContext inside, for example, your Index action controller, in case this is an IEnumerable model.

    0 讨论(0)
  • 2020-12-08 10:13

    You could also use deconstruction and tuples and try something like this:

    @foreach (var (index, member) in @Model.Members.Select((member, i) => (i, member)))
    {
      <div>@index - @member.anyProperty</div>
    
      if(index > 0 && index % 4 == 0) { // display clear div every 4 elements
          @: <div class="clear"></div>
      }
    }
    

    For more info you can have a look at this link

    0 讨论(0)
  • 2020-12-08 10:14

    Take a look at this solution using Linq. His example is similar in that he needed different markup for every 3rd item.

    foreach( var myItem in Model.Members.Select(x,i) => new {Member = x, Index = i){
        ...
    }
    
    0 讨论(0)
  • 2020-12-08 10:16
    //this gets you both the item (myItem.value) and its index (myItem.i)
    @foreach (var myItem in Model.Members.Select((value,i) => new {i, value}))
    {
        <li>The index is @myItem.i and a value is @myItem.value.Name</li>
    }
    

    More info on my blog post http://jimfrenette.com/2012/11/razor-foreach-loop-with-index/

    0 讨论(0)
提交回复
热议问题