简体   繁体   English

从asp.net mvc调用javascript

[英]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? 如何从mvc控制器调用javascript函数,类似于使用ICallbackHandler在webforms中执行的操作?

Is it possible? 可能吗? Thank you. 谢谢。

Controller actions cannot call javascript functions. 控制器动作无法调用javascript函数。 They return action results. 他们返回行动结果。 Javascript code should be contained on the client side. Javascript代码应该包含在客户端。 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. 因此,如果您想调用应在某些情况下执行的javascript函数,您可以订阅相应的事件,并在触发此事件时调用该函数。

For example if you wanted to call a javascript function when a button is clicked using jQuery you could do the following: 例如,如果您想在使用jQuery单击按钮时调用javascript函数,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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