2012-08-28 86 views
0

我最近一直在學習MVC 3,並且發現了兩種從我的列表視圖中刪除記錄的解決方案。不過,我希望他們兩個的組件,但我在JavaScript中缺乏知識使這非常困難。使用jquery-ui對話框的MVC3 Ajax鏈接確認對話框

我有這兩個鏈接刪除:

@Html.ActionLink("Delete", "Delete", 
    new { id = item.ID }, new { @class = "delete-link" }) | 
@Ajax.ActionLink("Delete Ajax", "Delete", "MyController", 
    new {id = item.ID}, 
    new AjaxOptions { 
     HttpMethod = "POST", 
     OnBegin = "return ConfirmDone()", 
     OnSuccess = "deleteConfirmation" 
    }) 

第一個使用下面的JavaScript刪除的記錄:

<script> 
    $(function() { 
     var deleteLinkObj; 
     // delete Link 
     $('.delete-link').click(function() { 
      deleteLinkObj = $(this); 
      $('#delete-dialog').dialog('open'); 
      return false; 
     }); 

     //definition of the delete dialog. 
     $('#delete-dialog').dialog({ 
      autoOpen: false, width: 400, resizable: false, modal: true, 
      buttons: { 
       "Continue": function() { 
        $.post(deleteLinkObj[0].href, function (data) 
        { 
         var rowId = "#myTableItem-id-" + data.id; 
         $('.myTable').find(rowId).hide('slow'); 
        }); 

        $(this).dialog("close"); 
       }, 
       "Cancel": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    }); 
</script> 

第二鏈路使用這個腳本功能進行確認:

<script> 
    function ConfirmDone() { 
     return confirm("Are you sure you want delete this item?"); 
    } 
</script> 

現在這兩種解決方案都可以正常工作,但我更喜歡編碼的第二個鏈接,但我喜歡jquery-ui在第一個鏈接中生成的確認框。所以我想將它們混合在一起。

我想我需要做的是,當Ajax.ActionLink呼叫ConfirmDone()然後我需要顯示一個jQuery對話框,因爲我對第一個鏈接。然而,我不確定如何產生這個,並允許這個對話框根據按下的按鈕返回true或false。

任何幫助將不勝感激。

非常感謝。

回答

0

嘗試的事情了幾個小時後,我已經想出了一個解決方案:

我的鏈接改成這樣:

@Ajax.ActionLink("Delete", "Delete", "StruContractUser", 
    new { id = item.UserID }, 
    new AjaxOptions { 
     HttpMethod = "Delete", 
     OnBegin = "JSONDeleteFile_OnBegin", 
     OnComplete = "notify" 
    }, 
new { @class = "delete-link" }) 

其中要求與OnBegin選擇此功能:

<script type="text/javascript"> 
    function JSONDeleteFile_OnBegin(context) { 
     return false; 
    } 
</script> 
+0

你的意思是返回'false'不顯示默認的確認對話框,並顯示你想要的jQuery對話框 – TheVillageIdiot