Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.73 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MVC Deleting record using Javascript pop up
  2. [HttpPost]
  3. public void DeleteItem(int id)
  4.        
  5. @for (int i = 0; i < Model.Items.Count; i++)
  6. {                            
  7.      <a href="#" class="delete-button" data-id="@Model.Items[i].Id">Delete</a>        
  8. }
  9.        
  10. $().ready(function () {
  11.     $(".delete-button").click(null, DeleteItem);  //DeleteItem is the callback      
  12.     return false;
  13. });
  14.        
  15. <div id="dialog-confirm" style="display:none;" title="Confirm">
  16. <p>
  17.     <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
  18.     Are you sure you want to proceed?
  19. </p>
  20. </div>
  21.        
  22. function confirmDeleteVersion() {        
  23.     var recordToDelete = $(this).attr("data-id"); // now we need the data-id to retrieve the identifier for the item to delete              
  24.     $("#dialog-confirm").dialog({
  25.         resizable: false,
  26.         height: 200,
  27.         modal: true,
  28.         buttons: {
  29.             "Delete": function () {        
  30.                 $(this).dialog("close");        
  31.                 $.post("/Home/Delete", { id : recordToDelete}, DeleteSuccessfull);
  32.             },
  33.             Cancel: function () {
  34.                 $(this).dialog("close");
  35.             }
  36.         }            
  37.     });    
  38. };
  39.        
  40. var recordToDelete = $(this).attr("data-id");
  41.        
  42. $.post("/Home/Delete", { id : recordToDelete}, DeleteSuccessfull);
  43.        
  44. function DeleteSuccessfull()
  45. {
  46.     //Do what you want...
  47. };
  48.        
  49. <script type="text/javascript">
  50.  
  51.     $(function () {
  52.         $(".delete").live("click", function (e) {
  53.             e.preventDefault();
  54.             if (confirm("Are you sure you wish to delete this article?")) {
  55.                 $.post(this.href);
  56.             }
  57.         });
  58.     });
  59. </script>
  60.  
  61. <a href="/Project/Delete/11" class="delete">Delete</a>