You need to enable JavaScript to run this app. ASP.NET Core 3.1 如何通过Ajax向模态框分部视图传值实现删除
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 3.1 如何通过Ajax向模态框分部视图传值实现删除

ASP.NET Core 3.1 传递待删除记录ID到模态框的实现方案

主视图调整

你当前写的代码存在的核心问题:asp-route-category是路由TagHelper,只会把参数拼到a标签的href属性上,但由于你是通过Bootstrap的data属性纯前端触发模态框、不发生页面跳转,这个参数根本无法被模态框读取,属于无效配置。
直接把删除按钮改成通过自定义属性存储当前行的ID和分类名即可:

<tbody>
  @{
    int rowCount = 1;
  }
  @foreach (var item in Model)
  {
    <tr>
      <td>@rowCount</td>
      <td>@item.CategoryTitle</td>
      <td>@item.IconCategory</td>
      <td>
        <a 
          data-toggle="modal" 
          data-target="#DeleteModal"
          data-id="@item.CategoryId"
          data-title="@item.CategoryTitle"
          title="delete" 
          class="btn btn-danger btn-xs"
        >
          删除
        </a>
      </td>
    </tr>
    rowCount++;
  }
</tbody>

分部视图(模态框载体)配置

你当前调用的分部视图@await Html.PartialAsync("_CreateMainCategory")不需要修改调用位置,只要在这个分部视图内部加入和按钮target匹配的删除模态框结构即可,确保模态框id和按钮的data-target值完全一致:

<!-- 与按钮data-target匹配的删除模态框 -->
<div class="modal fade" id="DeleteModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">删除确认</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        确认要删除分类:<strong id="deleteCategoryName"></strong> 吗?该操作不可恢复。
        <!-- 隐藏域存储待删除ID,提交时可直接随表单传到后端 -->
        <input type="hidden" id="deleteCategoryId" name="categoryId" />
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-danger" id="confirmDeleteBtn">确认删除</button>
      </div>
    </div>
  </div>
</div>

前端逻辑绑定

利用Bootstrap模态框自带的show.bs.modal事件(模态框打开前触发),把点击按钮上存储的自定义值填充到模态框对应元素里,不需要给每个删除按钮单独写重复的点击监听:

$(function () {
  // 监听删除模态框打开前的事件
  $('#DeleteModal').on('show.bs.modal', function (event) {
    // 获取触发模态框的源按钮
    const triggerButton = $(event.relatedTarget);
    // 取出按钮上存储的自定义参数
    const targetId = triggerButton.data('id');
    const targetTitle = triggerButton.data('title');
    // 把参数填充到模态框的对应元素
    const modal = $(this);
    modal.find('#deleteCategoryId').val(targetId);
    modal.find('#deleteCategoryName').text(targetTitle);
  });

  // 绑定确认删除按钮的提交逻辑
  $('#confirmDeleteBtn').on('click', function () {
    const deleteId = $('#deleteCategoryId').val();
    // 替换成你自己的后端删除接口地址即可
    $.post('/Category/Delete', { categoryId: deleteId }, function (result) {
      if (result.success) {
        $('#DeleteModal').modal('hide');
        location.reload();
      } else {
        alert('删除失败:' + result.message);
      }
    })
  })
})

注意事项

  • 不要在纯前端触发模态框的a标签上使用asp-route-xxx类的TagHelper属性,这类属性只会生成跳转用的href,不会被前端模态框逻辑读取
  • 检查模态框id和按钮的data-target值完全匹配,大小写不一致会导致模态框无法弹出
  • 可以直接通过模态框里的隐藏域携带ID提交表单,不需要额外在JS里手动拼接参数

内容的提问来源于stack exchange,提问作者AliKhansari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.26 21:36:03