检查foreach是否包含多于1个

时间:2015-08-21 07:46:23

标签: c# asp.net-mvc

这是我的观点:

<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标签下,所有日期都是属于八月属于八月。

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

我想要做到这样的事情。 如果组包含的时间超过1个月,请执行其他操作,直到日期为止。

1 个答案:

答案 0 :(得分:0)

在您的群组中添加变量:

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>