Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking count of model list items in razor

I am working with MVC 4 and using this model:

public class Cat {        

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

}

My view contains the corresponding Children list. I have a check in the Razor to see if Children is null:

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

I also check to see how many Children there are:

  @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 both span classes are shown. How can I only show one of the above spans if there are zero Children?

like image 634
shyama Avatar asked Dec 04 '14 05:12

shyama


1 Answers

Try this:-

@if(Model.Children != null){
   if(Model.Children.Count > 0){
     <span class="right-plus main-plus"><i class="fa fa-plus-square-o"></i></span>
   }
}
like image 183
Kartikeya Khosla Avatar answered Oct 21 '22 22:10

Kartikeya Khosla