2014-09-30 72 views
0

我想顯示一個確認對話框菜單,當用戶點擊DELETE按鈕。如何在asp.net mvc中的html.actionlink中調用javascript確認對話框函數?

但按鈕不會觸發我的腳本。觸發JavaScript後,如果用戶選擇確定,腳本必須執行該操作。否則,如果用戶點擊CANCEL,則不會發生任何事情。

這裏是JavaScript代碼;

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".confirmDialog").on("click", function (e) { 
      // e.preventDefault(); use this or return false 
      var url = $(this).attr('href'); 
      $("#dialog-confirm").dialog({ 
       autoOpen: false, 
       resizable: false, 
       height: 170, 
       width: 350, 
       show: { effect: 'drop', direction: "up" }, 
       modal: true, 
       draggable: true, 
       buttons: { 
        "OK": function() { 
         $(this).dialog("close"); 
         window.location = url; 
        }, "Cancel": function() { 
         $(this).dialog("close"); 
        } 
       } 
      }); 
      $("#dialog-confirm").dialog('open'); 
      return false; 
     }); 
    }); 
</script> 

這裏是MVC代碼;

<div style="text-align: right; font-weight: bold; font-size: larger; color: red;"> 
    <tr> 
     <td>@Html.ActionLink("Delete", "Your-Action", new { @class = "confirmDialog" })</td> 
    </tr> 
</div> 
<div id="dialog-confirm" style="display: none"> 
    <p> 
     <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span> 
     Are you sure ? 
    </p> 
</div> 
+0

請確保您的鏈接HTML看起來像您期望的那樣 – Andrei 2014-09-30 11:50:15

回答

2

的ActionLink的超載您正在使用預計第三個參數作爲objectroutes不htmlattributes。如圖所示

正確ActionLink的

@Html.ActionLink("Delete", "Your-Action", new{} ,new { @class = "confirmDialog" }) 

OR

@Html.ActionLink("Delete", "Your-Action", null ,new { @class = "confirmDialog" }) 

你的代碼的其餘部分就好了.. !!

+0

非常感謝。 – hislam 2014-09-30 14:36:38

+0

@hislam ...歡迎..如果答案解決了您的問題,然後接受答案,以便接受的答案幫助別人也.....謝謝。 – 2014-10-01 04:39:29