Getting index value on razor foreach

前端 未结 10 768

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)
                            {
                                
                                    
                                        @Html.DisplayFor(modelItem => item.DtCadastro)
                                    
    
                                    
                                        @Html.DisplayFor(modelItem => item.DsLembrete)
                                    
                                    
                                        @Html.DisplayFor(modelItem => item.DtLembrete)
                                    
                                    
                                        @{ 
                                            var contador = item.LembreteEnvolvido.Where(w => w.IdLembrete == item.IdLembrete).Count();
                                        }
                                        
                                        
                                    
                                    
                                        
                                    
                                
                            }
    

    do as coded:

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

    and use it like this:

    
    

    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.

提交回复
热议问题