开发者

Call javascript from asp.net mvc

How can i call a javascript function from a mvc controller similar to what you would do in webform开发者_StackOverflows 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
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜