“视图”页面中的“折叠”面板在“全部”按钮上显示第一条记录

collapse panel in view page is showing first record on all button

本文关键字:记录 按钮 显示 全部 一条 视图 折叠      更新时间:2023-09-26
    <table class="table" >
    @foreach (var item in Model)
    {
                <tr>
                    <td>
                    <a class="btn" data-toggle="collapse" data-target="#viewdetails_@item.Title">
                        @Html.DisplayFor(modelItem => item.Title)
                    </a>
                <div class="collapse" id="viewdetails_@item.Title">
                    <table>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.Category)</th>
                            <td>@Html.DisplayFor(modelItem => item.Category)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.Budget)</th>
                            <td>@Html.DisplayFor(modelItem => item.Budget)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.EstimatedHours)</th>
                            <td>@Html.DisplayFor(modelItem => item.EstimatedHours)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.SuggestedSources)</th>
                            <td>@Html.DisplayFor(modelItem => item.SuggestedSources)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.ProposedDeadline)</th>
                            <td>@Html.DisplayFor(modelItem => item.ProposedDeadline)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.Details)</th>
                            <td>@Html.DisplayFor(modelItem => item.Details)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.Comments)</th>
                            <td>@Html.DisplayFor(modelItem => item.Comments)</td>
                        </tr>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.Status)</th>
                            <td>@Html.DisplayFor(modelItem => item.Status)</td>
                        </tr>
                    </table>
                </div>
            </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Category)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Budget)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Details)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Status)
                    </td>
    </tr>
    }
    </table>

/* 以上是我的代码。我想在标题点击时折叠。有几个标题,但是在每个标题单击时,折叠窗口都会显示第一条记录。我应该怎么做才能显示相应的标题记录。

您在 for 循环中将相同的 id 传递给错误的元素,在您的页面中,每个锚标记都将具有数据目标视图详细信息,因此它将始终打开浏览器将在 DOM 中找到的第一个带有视图 ID 的div

像这样做:

<a class="btn" data-toggle="collapse" data-target="#viewdetails_@item.Id" onclick="">

在div 中:

<div class="collapse" id="viewdetails_@item.Id">

或者您可以使用列表项的 IndexOf 方法并将其与 id 一起传递:

在div 中:

<div class="collapse" id="viewdetails_@Model.IndexOf(item)">