简体   繁体   English

asp.net mvc 3从控制器动作调用javascript

[英]asp.net mvc 3 call javascript from controller action

this is what my controller action looks like - 这是我的控制器动作的样子 -

public void GetResizedImage(int height, int width, int GroupingId)
{
    //Generate image here
    image.RenderToStream();
    //here i need to execute "$("#modalDialog").dialog("option", "position", 'center');" to center my dialog after image is rendered.
}

In my client side, i am assigning the image src like this - 在我的客户端,我正在分配像这样的图像src -

var imgsrc = "Group/GetResizedImage/?height=" + height + "&width=" + width + "&GroupingId=" + GroupingId;
$("#ImageDiv").find("#MyImage").attr("src", imgsrc);

And the image is rendered on jquery dialog, so the dialog resizes and it gets hidden in the right side of browser, which creates horizontal scroll bar, and i dont want the scroll bar on my screen 并且图像在jquery对话框上呈现,因此对话框调整大小并隐藏在浏览器的右侧,这会创建水平滚动条,我不想在屏幕上显示滚动条

Is there anyway to center my jquery modal dialog after the image is rendered? 无论如何,在渲染图像后,我的jquery模式对话框是否居中? I know i used to use ScriptManager.RegisterClientScriptBlock in webforms but since this is MVC i cannot use it. 我知道我曾经在webforms中使用ScriptManager.RegisterClientScriptBlock ,但由于这是MVC我无法使用它。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

You can bind to the load event of the image. 您可以绑定到图像的load事件。 Here is some example code since you haven't included all your javascript (I haven't tested this but the load event is the general idea): 这是一些示例代码,因为您没有包含所有的javascript(我没有测试过这个,但是load事件是一般的想法):

var imgsrc = "Group/GetResizedImage/?height=" + height + "&width=" + width + "&GroupingId=" + GroupingId;
var img = $("#ImageDiv").find("#MyImage")
img.load(function() {
    // call your resize here
});
img.attr("src", imgsrc);

You cannot execute script from a controller action method. 您无法从控制器操作方法执行脚本。 Controller action methods execute on the server. 控制器操作方法在服务器上执行。 Javascript & jQuery execute on the browser. Javascript和jQuery在浏览器上执行。

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

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