[英]checking count of model list items in razor
I am working with MVC 4 and using this model: 我正在使用MVC 4并使用此模型:
public class Cat {
public string Name { get; set; }
public IEnumerable<Cat> Children {...}
}
My view contains the corresponding Children
list. 我的视图包含相应的
Children
列表。 I have a check in the Razor to see if Children
is null: 我在Razor检查是否有
Children
是空的:
@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: 我还查看有多少
Children
:
@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. 但如果count为0,则显示两个span类。 How can I only show one of the above spans if there are zero
Children
? 如果没有
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>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.