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
?
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>
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With