How to call ActionMethod from javascript?
I have a button that prompts user about his action(deleting smth). If user confirms, I want to execute ActionMethod
with postback
. How ca开发者_高级运维n i achieve that?
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');
}
});
精彩评论