gpt4 book ai didi

twitter-bootstrap - 使用 Bootstrap 模态确认删除列表项

转载 作者:行者123 更新时间:2023-12-03 23:39:35 25 4
gpt4 key购买 nike

我正在尝试使用 Bootstrap 模式创建删除确认。除了模式总是显示和删除列表中的第一项而不是单击删除按钮的实际列表项之外,一切都很好。有人可以看到我哪里出错了吗?

<table class="table">

@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.InvoiceID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.InvoiceDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DueDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Paid)
</td>
<td style="white-space: nowrap;width: 1px;">
<button type="button" class="btn btn-primary btn-xs" onclick="location.href='@Url.Action("Edit", "Billing", new { id = item.InvoiceID })'"><span class="glyphicon glyphicon-edit" style="vertical-align:middle;margin-top: -5px"></span> Edit</button>
<button type="button" class="btn btn-default btn-xs" onclick="location.href='@Url.Action("Index", "InvoiceItem", new { id = item.InvoiceID })'"><span class="glyphicon glyphicon-eye-open" style="vertical-align:middle;margin-top: -5px"></span> Details</button>
<button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal" data-id=@item.InvoiceID><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Confirm Delete</h4>
</div>
<div class="modal-body">
<!-- The problem seems to be here:-->
<p>Are you sure you want to delete invoice number: <b>@item.InvoiceID</b></p>
</div>
<div class="modal-footer">
<form asp-controller="Billing" asp-action="Delete" asp-route-id=@item.InvoiceID method="post" class="form-inline" role="form">
<button type="submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>
</td>
</tr>
}

上面的 View 是通过脚本加载的,以避免在更改下拉列表时使用以下代码刷新页面:

<script type="text/javascript">     


$(document).ready(function () {
loadPartial();
$("#ddlCategory").change(loadPartial);
});

function loadPartial() {
var companyID = $("#ddlCategory").val();
var displayPaid = $("#chkDisplayPaid").is(':checked');
var url = '@(Url.Action("InvoiceList", "Billing",null, null ))?companyID=' + companyID + '&displayPaid=' + displayPaid;
$("#InvoiceList").load(url);
}
</script>

最佳答案

您在循环中重复具有相同 ID 的相同模式,这是错误的,html 页面不能有重复的 ID,要么将您的模式 id="myModal" 更改为 id ="@(i + 1)"data-target="#(i +1 )"foreach 循环到 for 循环为 @for(var i = 0; i > model.count() ; i++)

但这种方法是错误的,因为您要为每一行重复模式。

取而代之的是,将模态框保留在表格之外并按如下方式执行:

@foreach (var item in Model)
{
......rest same code
<button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal" data-id=@item.InvoiceID><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
....... rest same code
}

将您的模式放在表格之外,并在其表格内添加带有 id 的输入隐藏字段。

<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Confirm Delete</h4>
</div>
<div class="modal-body">
<!-- The problem seems to be here:-->
<p>Are you sure you want to delete invoice number: <b id="InvoiceID"></b></p>
</div>
<div class="modal-footer">
<form asp-controller="Billing" asp-action="Delete" method="post" class="form-inline" role="form">
<input type="hidden" id="id">
<button type="submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" style="vertical-align:middle;margin-top: -5px"></span> Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>

show.bs.modal 事件在打开模式时触发。

 $(document).ready(function () {
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);//Button which is clicked
var clickedButtonId= button.data('Id');//Get id of the button
// set id to the hidden input field in the form.
$("#InvoiceID").text(clickedButtonId);
$("input #id").val(clickedButtonId);
});

*如果您愿意,对于编辑和详细信息,您可以在同一页面中打开相同或不同的模式。

关于twitter-bootstrap - 使用 Bootstrap 模态确认删除列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651570/

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