1

I have a button that prompts user about his action(deleting smth). If user confirms, I want to execute ActionMethod with postback. How can i achieve that?

Ilya Smagin
  • 5,992
  • 11
  • 40
  • 57
  • 2
    See http://stackoverflow.com/questions/764965/basic-ajax-example-with-asp-net-mvc/765974#765974 and http://stackoverflow.com/questions/1843017/sending-string-data-to-mvc-controller-using-jquery-ajax-and-post – Daveo Oct 14 '11 at 13:33

1 Answers1

2

Yes that is simple, you can do it this way

JS

$('#id-of-your-button').click(function() {
    if(confirm('Are you sure'))
         document.location = '/controller/action/id_to_delete';
});

or with the postback

$('#id-of-your-button').click(function() {
  if(!confirm('Are you sure'))
       return false;
});

if your button is not submit button than you can do it this way

$('#id-of-your-button').click(function() {
      if(confirm('Are you sure'))
           $('#id-of-your-form').submit();
      return false;
    });

if you don't have anything (form and submit button)

$('#id-of-your-button').click(function(){
  $.ajax({ url: '/controller/action', 
    dataType: 'html', 
    data: { id_to_delete: $('#where_are_you_holding_your_value').val() },
    type: 'post', 
    success: function(data) {
       alert('your item is deleted');
    }
});
Senad Meškin
  • 13,597
  • 4
  • 37
  • 55