发布时总是获取列表中第一项的值

always getting the value of first item in list when posting

asp-route-id=@item.Id 始终是列表中第一项的值,如果有人可以帮助我将 id 直接发送到模态,我将不胜感激

    @foreach (var item in Model.GetContracts)
    {   
<tr>
    <td>@item.Plate</td>
    <td>@item.Cname</td>

    <td>@item.CheckIn.ToString("dd/MM/yyyy")</td>
    <td>@item.CheckOut.ToString("dd/MM/yyyy")</td>
    <td>@item.Price</td>
    <td>@item.Paid</td>
    <td>@item.Deposit</td>
    <td>@item.Note</td>
    <td>@item.Id</td>
       
        
    <td><Button type="button" class="btn btn-success" data-toggle="modal" data-target="#Returned" onclick="getId">إغلاق العقد</Button></td>

    <td><a class="btn btn-warning" asp-page="Returned" asp-route-Id="@item.Id">إغلاق العقد</a></td>
    <td><a class="btn btn-warning" asp-page="CheckOut" asp-route-Id="@item.Id">تجديد</a></td>

    <td>
        <div class="modal fade" id="Returned" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                        <button type="button" cl ass="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form method="post" asp-page-handler="Returned" asp-route-id="@item.Id">
                            @item.Id



                            <input type="date" asp-for="@Model.Update" />

                            <input type="submit" class="btn btn-primary" />
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </td>

        
</tr>

    }

请帮忙 即使任何服务器端语言可能有帮助

您的模态实际上传递了正确的 id,但按钮不会触发正确的模态,因为 data-target 始终相同。这就是为什么你总是得到第一个 id 的模态。

添加一个索引来指定模态id:

@{  int i = 0; }   //add this...
@foreach (var item in Model.GetContracts)
{
    <tr>
        <td>@item.Plate</td>
        //....
        <td>@item.Id</td>
                                                            //change here...
        <td><Button type="button" class="btn btn-success" data-target="#Returned_@i" data-toggle="modal" onclick="getId">إغلاق العقد</Button></td>    
        <td>
                                     //change here...
            <div class="modal fade" id="Returned_@i" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                            <button type="button" cl ass="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <form method="post" asp-route-id="@item.Id">
                                @item.Id            
                                <input type="date" asp-for="@Model.Update" />  
                                <input type="submit" class="btn btn-primary" />
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </td>
    </tr>
    i++;    //add this...
}