Checking the number of items in the model list in the razor

I am working with mvc 4. I have a model

public class Cat {        

    public string Name { get; set; }         
    public IEnumerable<Cat> Children {...}

}

      

the list of models Model.Childern again contains the list cat.

I have a check in razor Model.Childern count is NULL (or some kind of list) and it is not. I use the following for this:

  @if (category.Children!=null)
  { 
     <span class="right-plus main-plus"><i class="fa fa-plus-square-o"></i></span>
  }

      

And also try

  @if (category.Children.Count()>0)
  { 
     <span class="right-plus main-plus"><i class="fa fa-plus-square-o"></i></span>
  }

      

But if count is 0, then this span class is also shown.

+5


source to share


1 answer


Try it:-



@if(Model.Children != null){
   if(Model.Children.Count > 0){
     <span class="right-plus main-plus"><i class="fa fa-plus-square-o"></i></span>
   }
}

      

+3


source







All Articles