2011-12-15 84 views
0

這是我的控制器的動作是什麼樣子 -asp.net的MVC 3調用JavaScript

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. 
} 

在我的客戶端,我分配圖片src這樣的 -

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

和圖像呈現在jQuery對話框中,所以對話框調整大小,它隱藏在瀏覽器的右側,這將創建水平滾動條,我不希望我的屏幕上滾動條

有沒有anywa在渲染圖像後,以中心我的jquery模式對話框? 我知道我曾經在webforms中使用ScriptManager.RegisterClientScriptBlock,但由於這是MVC,我無法使用它。 任何幫助,將不勝感激。 謝謝

+0

這不是一個操作方法,因爲它的返回類型爲void 。控制器操作方法必須返回ActionResult或ActionResult的子類。 – danludwig 2011-12-15 22:42:47

回答

2

您可以綁定到圖像的加載事件。下面是一些示例代碼,因爲你並沒有包括所有你的JavaScript(我沒有測試過這一點,但加載事件的總體思路):

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); 
+0

這真是個好主意!我希望我可以添加超過+1 – 2011-12-15 22:49:59

0

您不能從控制器操作方法執行腳本。控制器操作方法在服務器上執行。 Javascript & jQuery在瀏覽器上執行。

+0

這是不正確的。在asp.net中使用頁面對象我們使用從服務器發出JS命令。這是正確的,最終JS在瀏覽器中執行,但我們能夠從服務器端執行條件。要了解如何在asp.net mvc中執行操作,請訪問http://mazharkaunain.blogspot.com/2011/02/how-to-use-aspnet-mvc-javascriptresult.html – 2013-02-23 07:23:54