2

I want to use JQuery UI Dialog to confirm my delete action, i tried some tutorial on the internet but i still having the redirection to the delete page confirmation instead of showing the confirmation dialogue:

here my scripts implementation:

<link href="@Url.Content("~/Content/themes/base/minified/jquery-ui.min.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/jquery-ui-1.10.4.min.js")" type="text/javascript"></script>

   $("#dialog-confirm").dialog({
       autoOpen: false,
       resizable: false,
       width: 500,
       modal: true,
       buttons: {
           "Delete this item": function () {
               $(this).dialog("close");
           },
           Cancel: function () {
               $(this).dialog("close");
           }
       }
   });

   $("[data-dialog-confirm]").click(function (e) {
       e.preventDefault();
       var theHREF = $(this).attr("href");
       $("#dialog-confirm").dialog('option', 'buttons', {
           "Yes":
           function () {
               $.get(theHREF, null, function () { refreshList() });
               $(this).dialog("close");
           }, "No":
         function () { $(this).dialog("close"); }
       });
       $("#dialog-confirm").dialog("open");
   });

my confirmation div :

  <div id="dialog-confirm" title="Delete the item?">
            <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be deleted. Are you sure?</p>
        </div> 

and my action link that invoke the delete confirmation dialogue

 @Html.Raw(@Html.ActionLink(".", "DeleteDemandeLocation", new { id = item.Publication_ID }, new { data_dialog_confirm = "true" }).ToHtmlString().Replace(".", "<img src=\"/Content/Images/Delete.png\" style=\"margin-left:0px; width:19px; height:19px\" />")) |

Finally my delete action/controller side :

 //
    // GET: /DemandeLocation/Delete/5
    public ActionResult DeleteDemandeLocation(int id)
    {
        return View(db.PublicationSet.Find(id));
    }

    //
    // POST: /DemandeLocation/Delete/5
    [HttpPost]
    public ActionResult DeleteDemandeLocation(int id, DemandeLocation demandeLocation)
    {
        try
        {
            var demandeLocationGetById = db.DemandeLocations.Find(id);
            if (demandeLocationGetById != null)
                db.DemandeLocations.Remove(demandeLocationGetById);

            db.SaveChanges();

            return RedirectToAction("ListDemandeLocation");
        }
        catch
        {
            return RedirectToAction("Error");
        }
    } 

2 Answers 2

2

You have forgotten to return false from the click handler

$("[data-dialog-confirm]").click(function (e) {
   e.preventDefault();
   var theHREF = $(this).attr("href");
   $("#dialog-confirm").dialog('option', 'buttons', {
       "Yes":
       function () {
           $.get(theHREF, null, function () { refreshList() });
           $(this).dialog("close");
       }, "No":
     function () { $(this).dialog("close"); }
   });
   $("#dialog-confirm").dialog("open");
    return false;

});

Sign up to request clarification or add additional context in comments.

2 Comments

i've modified the function by adding the return! but once i click on the delete icon, it redirect me to another view : localhost:50412/DemandeLocation/DeleteDemandeLocation instead of showing the confirmation dialogue
If you put a debug point in the click handler, does it get hit? can you confirm that the return false is actually hit?
1

Try this:

 $(document).ready(function(){
         $("#dialog-confirm").dialog({
               autoOpen: false,
               resizable: false,
               width: 500,
               modal: true,
               buttons: {
                   "Delete this item": function () {
                       $(this).dialog("close");
                   },
                   Cancel: function () {
                       $(this).dialog("close");
                   }
               }
           });

           $("[data-dialog-confirm]").click(function (e) {
               e.preventDefault();
               var theHREF = $(this).attr("href");
               $("#dialog-confirm").dialog('option', 'buttons', {
                   "Yes":
                   function () {
                       $.get(theHREF, null, function () { refreshList() });
                       $(this).dialog("close");
                   }, "No":
                 function () { $(this).dialog("close"); }
               });
               $("#dialog-confirm").dialog("open");
           });
      });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.