2017-07-15 66 views
2

在開始我想爲我的英語不好的技能感到抱歉。我使用實體框架使用MVC創建Web應用程序。我想創建一個定製的確認對話框,當用戶嘗試從表中刪除記錄。當我使用標準確認對話框「確認('確定?')」時,確認工作。但我想使用自定義的確認對話框,如自舉或類似的甜蜜警報。在控制器C#,MVC確認刪除對話框

刪除方法:

 [HttpPost] 
    [ValidateAntiForgeryToken] 
    [Authorize] 
    public ActionResult Delete(int id) 
    { 
     DiabeticControl diabeticControl = db.DiabeticControls.Find(id); 
     db.DiabeticControls.Remove(diabeticControl); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 


    } 

表鑑於:

@foreach (var item in Model) 
{ 
     <tr> 
      <td>@Html.DisplayFor(modelItem => item.Result)</td> 
      <td>@Html.DisplayFor(modelItem => item.Time)</td> 
      <td> 
       <div class="form-group"> 
        @using (Html.BeginForm("Delete", "DiabeticControls", new { id = item.Id })) 
        { 
          @Html.AntiForgeryToken() 
          @Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-default xs-margin" }) 
          @Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-default xs-margin" }) 
          <button type="submit" class="btn btn-danger xs-margin delete" >Delete</button> 

        } 
       </div> 
       @{ 
        ViewBag.Key = item.Id; 
       } 


      </td> 

      <td>@Html.HiddenFor(modelItem => item.UserId)</td> 
     </tr> 
    } 

我已經找到了簡單的問題:enter link description here但它並不在我的項目工作。

而且我的JavaScript:

@section腳本{

<script type="text/javascript"> 

    $(function() { 

     $('.delete').on('click', function (e, data) { 
      if (!data) { 
       handleDelete(e, 1); 
      } else { 
       window.location = $(this).attr("@Url.Action("DiabeticControls")"); 
      } 
     }); 

    }); 


    function handleDelete(e, stop) { 
     if (stop) { 
      e.preventDefault(); 
      swal({ 
       title: "Are you sure?", 
       text: "You will not be able to recover the delaer again!", 
       type: "warning", 
       showCancelButton: true, 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Yes, delete!", 
       closeOnConfirm: false 
      }, 
      function (isConfirm) { 
       if (isConfirm) { 
        swal("Deleted", "", "success"); 
        $('.delete').trigger('click', {}); 
       } 
       else { 
        swal("Cancelled", "", "error"); 
       } 
      }); 
     } 
    }; 





</script> 

}

+0

請發表你的JavaScript –

+0

好吧,我添加JavaScript來主哨;) – MarMar1236

+0

我看到對話框,但即使我點擊是的,沒有什麼會發生。 – MarMar1236

回答

1

好,非常感謝很多的幫助,但我找到了解決辦法。問題出在我附加的圖書館裏。我有不同的版本的jquery.js,sweetalert.css和sweetalert.js取代它,我定製基於這個職位的解決方案我的JavaScript:enter link description here

我的javascript:

@section腳本{

<script type="text/javascript"> 

    $(document).ready(function() { 

     $('.delete').on('click', function (e, data) { 
      if (!data) { 
       handleDelete(e, 1); 
      } else { 
       window.location = $(this).attr('href'); 
      } 
     }); 



    }); 


    function handleDelete(e, stop) { 
     if (stop) { 
      e.preventDefault(); 
      swal({ 
       title: "Are you sure?", 
       text: "You will not be able to recover the delaer again!", 
       type: "warning", 
       showCancelButton: true, 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Yes, delete!", 
       closeOnConfirm: false 
      }, 
      function (isConfirm) { 
       if (isConfirm) { 
        $(e.target).trigger('click', {}); 
       } 
      }); 
     } 
    }; 





</script> 

}