【问题标题】:Delete Confirm with Bootstrap modal in Asp.Net MVC在 Asp.Net MVC 中使用 Bootstrap 模式删除确认
【发布时间】:2016-07-11 15:24:30
【问题描述】:

我正在尝试使用引导模式进行对话以确认删除。删除效果很好,除了它没有得到我选择的数据,而是从数据库中按 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并使用数据属性,直到没有其他选择

【问题讨论】:

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


    【解决方案1】:

    无论您尝试删除哪些数据,这种方式的模态都具有相同的 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-->
    

    【讨论】:

      【解决方案2】:

      实际上,您可能想在自己的观点中解决很多问题。您正在遍历模型中的项目,并为每个项目创建一个单独的表单(和模态)。这可能并不理想,但是如果您真的想这样做,您可以在模态的 html 中添加对项目 ID 的引用。只需添加一个隐藏输入并将其值设置为 item.id。

      但是,我不推荐这种方法。我不确定你想要回避 JavaScript 的原因,但你想在这里创建的功能实际上是非常基本的。

      看到这个帖子:Confirm delete modal/dialog with Twitter bootstrap?

      编辑:

      @foreach (var item in Model)
      {    
      <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-item-id="@item.ID" data-item-description="@item.Report.Description" data-toggle="modal" data-target="#confirm-delete">Delete</button>                
          </td>
      </tr>    
      }
      
      <div class="modal fade" id="confirm-delete" 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 class="description"></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>
      

      <script>
      $('#confirm-delete').on('click', '.btn-ok', function(e) {
          var $modalDiv = $(e.delegateTarget);
          var id = $(this).data('itemId');        
          $modalDiv.addClass('loading');
          $.post('/RepFilters/Delete/' + id).then(function () {
              $modalDiv.modal('hide').removeClass('loading');
          });
      });
      $('#confirm-delete').on('show.bs.modal', function(e) {
          var data = $(e.relatedTarget).data();
          $('.description', this).text(data.itemDescription);
          $('.btn-ok', this).data('itemId', data.itemId);
      });
      </script>
      

      【讨论】:

      • 你能告诉我如何在 Asp.Net MVC 中使用的代码吗?
      • 所以你说最好使用javascript而不是避免它的好习惯?
      • 不,我不建议尝试避免使用 JavaScript。
      【解决方案3】:

      您首先在 jquery 中编写一个删除函数。为了显示确认消息,您可以使用 sweetalert 并为 sweetalert 编写一个自定义文件。

      你必须在你的视图页面中添加 refrence sweetalert css 和脚本。

      function Delete(id) {
                 var submitdelete=function(){  $.ajax({
                          url: '@Url.Action("/mycontroller/Delete)',
                          type: 'Post',
                          data: { id: id }
                      })
                      .done(function() {
                          $('#' + id).remove();//if you want to delete table row
                         msgBox.success("Success","Ok");
                     });}
                  msgBox.okToContinue("warning", "Are you sure to delete ?", "warning", "ok","cancel", submitdelete);
      
              }

      确认对话框

        
      var msgBox = {
          message: {
              settings: {
                  Title: "",
                  OkButtonText: "",
                  type:"info"
              }
          },
          okToContinue: function(title, text, type, okButtonText,closeButtonText, isConfirmDo) {
              swal({
                      title: title,
                      text: text,
                      type: type,
                      showCancelButton: true,
                      confirmButtonClass: 'btn-danger',
                      confirmButtonText: okButtonText,
                      cancelButtonText: closeButtonText,
                      closeOnConfirm: false,
                      closeOnCancel: true
                  },
                  function(isConfirm) {
                      if (isConfirm) {
      
                          isConfirmDo();
                      }
                  });
          },
         
          confirmToContinue: function(title, text, type, confirmButtonText, cancelButtonText, isConfirmDo, isNotConfirmDo, showLoader) {
              if (!showLoader) {
                  showLoader = false;
              }
      
              swal({
                      title: title,
                      text: text,
                      type: type,
                      showCancelButton: true,
                      confirmButtonColor: "#DD6B55",
                      confirmButtonText: confirmButtonText,
                      cancelButtonText: cancelButtonText,
                      closeOnConfirm: true,
                      closeOnCancel: true,
                      showLoaderOnConfirm: showLoader
      
                  },
                  function(isConfirm) {
                      if (isConfirm) {
                          isConfirmDo();
                      }
                  });
          } ,
      
          success: function (title, text,okButtontex) {
              
              swal({
                  title: title,
                  text: text,
                  type: "success",
                  confirmButtonText: okButtontex
              });
      
          },
          info: function (title, text) {
      
              swal({
                  title: title,
                  text: text,
                  type: "info",
                  confirmButtonText: "OK"
              });
      
          },
          warning: function (title, text) {
      
              swal({
                  title: title,
                  text: text,
                  type: "warning",
                  confirmButtonText: "OK"
              });
      
          },
          error: function (title, text) {
      
              swal({
                  title: title,
                  text: text,
                  type: "error",
                  confirmButtonText: "OK"
              });
      
          },
      
      }
      
      
       

      【讨论】:

      • 但在引导文档中它说我可以避免使用 javascript,而是使用数据属性。这就是我要找出的,哪个属性传递了我需要的数据
      【解决方案4】:

      这里有一个简单的方法来做到这一点。您应该能够根据您的情况调整我在这里所做的事情。这需要很少的 JavaScript 代码。

      <script>
          var path_to_delete;
          var root = location.protocol + "//" + location.host;
      
          $("#deleteItem").click(function (e) {
              path_to_delete = $(this).data('path');
              $('#myform').attr('action', root + path_to_delete);
          });
      </script>
      <table class="table table-hover" id="list">
          <thead class="bg-dark text-white">
              <tr>
      
                  <th>
                      Edit
                  </th>
                  <th>
                      Employee
                  </th>
                  <th>
                      Effective Date
                  </th>
                  <th>
                      ST/OT/DT
                  </th>
                  <th>
                      Other Pay
                  </th>
                  <th>
                      Job
                  </th>
                  <th>
                      Pending?
                  </th>
                  <th>
                      Delete
                  </th>
              </tr>
          </thead>
          <tbody>
              @foreach (var item in Model)
              {
                  <tr>
                      <td>
                          <a class="btn btn-sm" href="~/Employees/TerminationEdit/@item.Employee_Termination_Info_Id">
                              <i class="fa fa-lg fa-pencil-alt text-dark"></i>
                          </a>
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Employee_Name_Number)
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Effective_Date)
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Employee_Time)
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Employee_Other_Pay)
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Job_Name)
                      </td>
                      <td>
                          @Html.DisplayFor(modelItem => item.Pending)
                      </td>
                      <td>
                          <a id="deleteItem" class="btn btn-sm" data-toggle="modal" data-target="#deleteModal"
                             data-path="/Employees/TerminationDelete/@item.Employee_Termination_Info_Id">
                              <i class="fa fa-lg fa-trash-alt text-danger"></i>
                          </a>
                      </td>
                  </tr>
              }
          </tbody>
      </table>
      
      
      <!-- Logout Modal-->
      <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
              <div class="modal-content">
                  <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabel">Are you sure?</h5>
                      <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">×</span>
                      </button>
                  </div>
                  <div class="modal-body">Are you sure you want to delete this termination record? <br /><span class="text-danger">This cannot be undone.</span></div>
                  <div class="modal-footer">
                      <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                      @using (Html.BeginForm("TerminationDelete", "Employees", FormMethod.Post, new { id = "myform", @class = "" }))
                      {
                          @Html.AntiForgeryToken()
                 
                          <input type="submit" value="Delete" id="submitButton" class="btn btn-danger" />
                      }
                  </div>
              </div>
          </div>
      </div>

      所以这里发生的情况是,页面将循环浏览模型并绘制删除按钮(使用字体 awesome)。请注意,这里是设置 data-path 属性以供以后使用。单击按钮时,它会立即为模式弹出窗口上的按钮设置表单操作。这很重要,因为它在删除按钮周围有一个表单,它会将其发送到 POST 而不是 GET,正如 Rasika 和 Vasil Valchev 指出的那样。此外,它还具有防伪令牌的好处。

      【讨论】:

        猜你喜欢
        • 2018-07-02
        • 2015-09-29
        • 1970-01-01
        • 2014-03-23
        • 2022-08-14
        • 1970-01-01
        • 2014-07-21
        • 1970-01-01
        • 2017-01-19
        相关资源
        最近更新 更多