gpt4 book ai didi

asp.net-mvc - 在 Asp.Net MVC 中使用 Bootstrap 模式删除确认

转载 作者:行者123 更新时间:2023-12-05 01:10:12 25 4
gpt4 key购买 nike

我正在尝试使用 Bootstrap 模式进行对话以确认删除。删除效果很好,除了它没有得到我选择的数据,而是从数据库中按 ID 顺序获取第一个数据。我是客户端编程的新手,所以如果有人可以帮助我,那就太好了。

代码是:

[HttpPost]
public async Task<ActionResult> Delete(int id)
{
RepFilter repFilter = await db.RepFilters.FindAsync(id);
db.RepFilters.Remove(repFilter);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}


(razor)
@foreach (var item in Model)
{
using (Html.BeginForm("Delete", "RepFilters", new { id = item.ID }))
{
<tr>
<td>@index</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Report.Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal">Delete</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
</div>
<div class="modal-body">Are you sure you want to delete: <span><b>@item.Description</b></span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" value="Delete" class="btn btn-danger" />
</div>
</div>
</div>
</div>
</td>
</tr>

}
}
</tbody>

打开模态的按钮获得了正确的 ID,但模态没有!

那么如何让modal取到足够的数据来删除呢?

我试图避免编写 javascript 并使用数据属性,直到没有其他选择

最佳答案

无论您尝试删除哪些数据,这种方式的模态都具有相同的 ID。
所以只需添加一个变量来为 mmodal 指定不同的 ID:

 using (Html.BeginForm("Delete", "RepFilters", new { id = item.ID }))
{
var myModal = "myModal" + item.ID;
<tr>
<td>...</td>
<td>...</td>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#@myModal">Delete</button>
<!-- Modal -->
<div class="modal fade" id="@myModal" tabindex="-1" role="dialog" data-keyboard="false" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-sm">
...........<!--And the rest of the modal code-->

关于asp.net-mvc - 在 Asp.Net MVC 中使用 Bootstrap 模式删除确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com