简体   繁体   中英

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.

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
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM