asp.net-mvc - MVC Actionlink 和 Bootstrap 模态提交

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

我正在开发一个 MVC 5 Web 应用程序。在我的一个 Razor View 中,我有一张表,其中显示了几行数据。每行数据旁边都有一个删除按钮。当用户单击删除按钮时,我希望弹出 Bootstrap Modal 并要求用户确认删除。

@foreach (var item in Model.Data) {
<tr>
 <td>...</td>
 <td>@Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "btn btn-danger btn-xs", data_toggle = "modal", data_target = "#myModal" })</td>
</tr>
}

事实上,当用户单击“删除”按钮时,模态框会弹出,但我似乎无法将 Actionlink 参数中的 ID 传递给模态框内的“确认”按钮,以便将其发送到我的 Controller 中的删除操作。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
             <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
             <h4 class="modal-title" id="myModalLabel">Delete Nomination</h4>
         </div>
         <div class="modal-body">
           Are you sure you wish to delete this nomination?
         </div>
         <div class="modal-footer">
           <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
           <button type="button" id="mySubmit" class="btn btn-primary">Confirm</button>
         </div>
       </div>
      </div>
</div>

有人可以帮忙吗?

谢谢。

最佳答案

<script type="text/javascript">

    //Everytime we press delete in the table row
    $('.delete').click(function(e) {
        e.preventDefault();

        //Update the item to delete id so our model knows which one to delete
        var id = $(this).data('id');
        $('#item-to-delete').val(id);

    });


    //Everytime we press sumbit on the modal form...
    $('#mySubmit').click(function() {

        //Get the id to delete from the hidden field
        var id = $('#item-to-delete').val();


        //Call our delete actionresult and pass over this id
        $.post(@Url.Action("Delete", "Delete"), { id : id } , function (data) {

            alert("Deleted");

        });


    });

</script>

还有你的 html...

@Html.Hidden("item-to-delete", "", new { @id = "item-to-delete"})
@foreach (var item in Model.Data) {
<tr>
    <td>...</td>

    <td><a href="" class="btn btn-danger btn-xs delete" data-toggle= "modal" data-target="#myModal" data-id="@item.id">Delete</a></td>

</tr>
}

我认为你的 Controller 操作是这样的......

public ActionResult Delete(Guid id)
{


}

关于asp.net-mvc - MVC Actionlink 和 Bootstrap 模态提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23831745/

相关文章:

jquery - 禁用剑道网格中的编辑

javascript - 按钮单击事件在 Jquery 中不起作用

html - 第一次加载页面时如何使用jquery在移动 View 中隐藏侧边栏

c# - 在 ASP.NET MVC 中获取负载

javascript - jqGrid:为什么我为网格编辑定义的事件没有触发?

c# - 从 varbinary 数据转换的图像显示空白图像如下图 1 所示

javascript - Bootstrap 始终显示子菜单

jquery - Bootstrap 模态点击时显示空白屏幕

jquery - div 元素之间的空间随着每个新的 div 呈指数级增长

javascript - 如何用html页面替换模态对话框?我需要重定向到 html 页面而不使用模式对话框