使用Bootstrap模式确认删除列表项

时间:2016-01-07 09:30:25

标签: twitter-bootstrap asp.net-core bootstrap-modal asp.net-core-mvc

我正在尝试使用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>
}

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

<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> 

1 个答案:

答案 0 :(得分:4)

你在循环中重复同一个带有相同id的模态是错误的,html页面不能有重复的id,要么你的模态id =“myModal”改为id =“@(i + 1)”并且data-target =“#(i + 1)”和“foreach”循环到“for”循环为@for(var i = 0; i&gt; 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);
    });    

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