+9  A: 

You could do it like this:

  • mark the <a> with a class, say "cancel"
  • set up the dialog by acting on all elements with class="cancel":

    $('a.cancel').click(function() { 
      var a = this; 
      $('#myDialog').dialog({
     buttons: {
       "Yes": function() {
       window.location = a.href; 
       }
     }
      }); 
      return false;
    });
    

(plus your other options)

The key points here are:

  • make it as unobtrusive as possible
  • if all you need is the URL, you already have it in the href.

However, I recommend that you make this a POST instead of a GET, since a cancel action has side effects and thus doesn't comply with GET semantics...

Mauricio Scheffer
Thanx for a good answer. I'm will try it out, one question though. you mention that it's better to make it a POST instead of a GET which implies that a regular href like href="/Booking.aspx/Cancel/10" would be a GET ist that right? and if so what would it lool like to make it a post then?
Frederik
In order to make it a post, instead of changing the window.location, you could use jQuery $.post() ajax function.See http://docs.jquery.com/Ajax/jQuery.post#examples
Franck
I wouldn't use $.post(), that approach doesn't degrade well. Just write a standard <form> without any ajax, make it work without confirmation, and *then* add the confirmation "on top" of your <form>
Mauricio Scheffer
This is also known as the hijax approach (http://domscripting.com/blog/display/41)
Mauricio Scheffer
+2  A: 

In terms of what you are doing with jQuery, my understanding is that you can chain functions like you have and the inner ones have access to variables from the outer ones. So is your ShowDialog(x) function contains these other functions, you can re-use the x variable within them and it will be taken as a reference to the parameter from the outer function.

I agree with mausch, you should really look at using POST for these actions, which will add a <form> tag around each element, but make the chances of an automated script or tool triggering the Cancel event much less likely. The Change action can remain as is because it (presumably just opens an edit form).

Falkayn
A: 

Thanx guys for your good advice.

Frederik
+1  A: 

I have now tried your suggestions and found that it kinda works,

  1. The dialog div is alsways written out in plaintext
  2. With the $.post version it actually works in terms that the controller gets called and actually cancels the booking, but the dialog stays open and page doesn't refresh. With the get version window.location = h.ref works great.

Se my "new" script below:

$('a.cancel').click(function() {
        var a = this;               
        $("#dialog").dialog({
            autoOpen: false,
            buttons: {
                "Ja": function() {
                    $.post(a.href);                     
                },
                "Nej": function() { $(this).dialog("close"); }
            },
            modal: true,
            overlay: {
                opacity: 0.5,

            background: "black"
        }
    });
    $("#dialog").dialog('open');
    return false;
});

});

Any clues?

oh and my Action link now looks like this:

<%= Html.ActionLink("Cancel", "Cancel", new { id = v.BookingId }, new  { @class = "cancel" })%>
Frederik
See my comments on my answer about using $.post() and the hijax approach
Mauricio Scheffer
A: 

Ok the first issue with the div tag was easy enough: I just added a style="display:none;" to it and then before showing the dialog I added this in my dialog script:

$("#dialog").css("display", "inherit");

But for the post version I'm still out of luck.

Frederik
See my comments on my answer about using $.post() and the hijax approach
Mauricio Scheffer
+1  A: 

Looking at your code what you need to do is add the functionality to close the window and update the page. In your "Yes" function you should write:

        buttons: {
            "Ja": function() {
                $.post(a.href);
                $(a). // code to remove the table row
                $("#dialog").dialog("close");
            },
            "Nej": function() { $(this).dialog("close"); }
        },

The code to remove the table row isn't fun to write so I'll let you deal with the nitty gritty details, but basically, you need to tell the dialog what to do after you post it. It may be a smart dialog but it needs some kind of direction.

thaBadDawg
Thanx for your answer. I will try it out and also find a way to remove the row...
Frederik
I was thinking about it, if you add an id to the '<TR>' tag then you might be able to get jQuery to remove that row easily enough.
thaBadDawg
A: 

Just give you some idea may help you, if you want fully control dialog, you can try to avoid use of default button options, and add buttons by yourself in your #dialog div. You also can put data into some dummy attribute of link, like Click. call attr("data") when you need it.

ok, thanx I will try it out.
Frederik
+2  A: 

After SEVERAL HOURS of try/catch I finally came with this working example, its working on AJAX POST with new rows appends to the TABLE on the fly (that was my real problem):

Tha magic came with link this:

<a href="#" onclick="removecompany(this);return false;" id="remove_13">remove</a>
<a href="#" onclick="removecompany(this);return false;" id="remove_14">remove</a>
<a href="#" onclick="removecompany(this);return false;" id="remove_15">remove</a>

This is the final working with AJAX POST and Jquery Dialog:

  <script type= "text/javascript">/*<![CDATA[*/
    var $k = jQuery.noConflict();  //this is for NO-CONFLICT with scriptaculous
     function removecompany(link){
        companyid = link.id.replace('remove_', '');
    $k("#removedialog").dialog({
          bgiframe: true,
          resizable: false,
          height:140,
          autoOpen:false,
          modal: true,
          overlay: {
           backgroundColor: '#000',
           opacity: 0.5
          },
          buttons: {
           'Are you sure ?': function() {
            $k(this).dialog('close');
            alert(companyid);
            $k.ajax({
                  type: "post",
                     url: "../ra/removecompany.php",
                     dataType: "json",
                     data: {
                       'companyid' : companyid
                           },
                     success: function(data) {
                           //alert(data);
                           if(data.success)
                           {
                            //alert('success'); 
                            $k('#companynew'+companyid).remove();
                           }
                 }
            }); // End ajax method
           },
           Cancel: function() {
            $k(this).dialog('close');
           }
          }
         });
         $k("#removedialog").dialog('open'); 
         //return false;
     }
    /*]]>*/</script>
    <div id="removedialog" title="Remove a Company?">
        <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        This company will be permanently deleted and cannot be recovered. Are you sure?</p>
    </div>
Carlitux
+4  A: 

jQuery provides a method which store data for you, no need to use a dummy attribute or to find workaround to your problem.

Bind the click event:

$('a[href*=/Booking.aspx/Change]').bind('click', function(e) {
    e.preventDefault();
    $("#dialog-confirm").data('link', this).dialog('open');
});

And your dialog:

$("#dialog-confirm").dialog({
    autoOpen: false,
    resizable: false,
    height:200,
    modal: true,
    buttons: {
        Cancel: function() {
            $(this).dialog('close');
        },
        'Delete': function() {
            $(this).dialog('close');
            var path = $(this).data('link').href;
            $(location).attr('href', path);
        }
    }
});
Boris Guéry