检查foreach是否大于1

本文关键字:大于 是否 foreach 检查 | 更新日期: 2023-09-27 18:08:59

这是我的观点:

<form class="form-horizontal">
<div class="portlet-body form">
    <div class="form-group">
        @{
            int i = 0;
        }
        @foreach (var group in ((List<DateTime>)ViewBag.MissingDays).GroupBy(x => x.Month))
        {
           i++;
           var month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(group.Key);
            <div class="panel-group accordion" id="accordion1">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_@i">
                                @month
                            </a>
                        </h4>
                    </div>
                    <div id="collapse_@i" class="panel-collapse collapse">
                        <div class="panel-body">
                            <div class="col-md-12">
                                @foreach (var date in group)
                                {
                                    var isoDate = date.ToString("yyMMdd");
                                    <label>
                                        <input type="checkbox" id="isoDate" name="isoDate" value="@isoDate" />
                                        @date.ToString("ddd").Substring(0, 2)
                                        @isoDate
                                    </label>
                                }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

我在这里做的是将日期按月份分组,然后我有一个foreach来打印正确月份下的所有日期,所以所有属于may的日期都在may标签下,所有属于august的日期都在august标签下。

但是我想做的是,如果只有1个月,我不想打印出一个带有月份的标签,我只想打印出日期。

我想把它做成这样。如果一个组包含超过1个月的时间,那么就这样做。

检查foreach是否大于1

在组中添加一个变量:

var groups = ((List<DateTime>)ViewBag.MissingDays).GroupBy(x => x.Month)

然后你可以检查它的计数:

<form class="form-horizontal">
<div class="portlet-body form">
<div class="form-group">
    @{
        int i = 0;
    }
    @foreach (var group in groups)
    {
       i++;
       var month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(group.Key);
        <div class="panel-group accordion" id="accordion1">
            <div class="panel panel-default">
                @if(groups.Count() > 1)
                {
                <div class="panel-heading">
                    <h4 class="panel-title">
                        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_@i">
                            @month
                        </a>
                    </h4>
                </div>
                }
                <div id="collapse_@i" class="panel-collapse collapse">
                    <div class="panel-body">
                        <div class="col-md-12">
                            @foreach (var date in group)
                            {
                                var isoDate = date.ToString("yyMMdd");
                                <label>
                                    <input type="checkbox" id="isoDate" name="isoDate" value="@isoDate" />
                                    @date.ToString("ddd").Substring(0, 2)
                                    @isoDate
                                </label>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    }
</div>