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?

1

1 Answer 1

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');
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

What if it is post method(and it obviously has to be be) ?
I have fixed code, look at it now, if your button is submit button of your form
What if i don't have submit button?

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.