Andr
Andr

Reputation: 627

Call javascript from asp.net mvc

How can i call a javascript function from a mvc controller similar to what you would do in webforms with ICallbackHandler?

Is it possible? Thank you.

Upvotes: 0

Views: 733

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039238

Controller actions cannot call javascript functions. They return action results. Javascript code should be contained on the client side. So if you want to call a javascript function that should execute under certain circumstances you could subscribe for the corresponding event and when this event is triggered call the function.

For example if you wanted to call a javascript function when a button is clicked using jQuery you could do the following:

$(function() {
    // subscribe for the click event
    $('#someId').click(function() {
        // the button is clicked => execute some javascript function here
    });
});

Upvotes: 2

Related Questions