Bootstrap collapse within a foreach loop

前端 未结 1 1985
轮回少年
轮回少年 2021-01-18 17:40

I am having trouble getting the collapsible panels to work within my foreach loop. When an item is clicked, all of the items expand/retract, which isn\'t what I want. I wa

相关标签:
1条回答
  • 2021-01-18 18:11

    Basically you are creating panel with loop and assigning the same id to all the panel-group and that's what causing the problem here! So you can try working as below and please note ids should be unique in DOM

    @{int i=0;}
    foreach (var item in groupItem)
    {
           <div class="panel-group" id="accordion_@i">
                  <div class="panel panel-default" id="panel_@i">
                         <div class="panel-heading">
                               <h4 class="panel-title">
                                     <a data-toggle="collapse" data-target="#collapseOne_@i" href="#collapseOne_@i">
                                           @Html.DisplayFor(modelItem => item.Name)
                                      </a>
                                </h4>
                          </div>
                          <div id="collapseOne_@i" class="panel-collapse collapse in">
                                <div class="panel-body">
                                     @Html.DisplayFor(modelItem => item.IdNumber)
                                </div>
                          </div>
                  </div>
            </div>
            i++;
    }
    
    0 讨论(0)
提交回复
热议问题