2010-08-31 213 views
3

基本上我想要一個按鈕,在包含我的http://fake.com/Report/PageFixes...頁面的新窗口中打開一個頁面。目前我有一個表單中的按鈕,但如果有更好的方法來做到這一點,那麼我也爲此而努力。使用MVC打開一個新窗口

<% using (Html.BeginForm("PageFixes", "Report", new { id = 9 }, FormMethod.Get, new { onSubmit="window.open()" })) %> 
<% { %> 
    <%= Html.CSButton(new ButtonViewData() { Text = "Submit" })%> 
<% } %> 

回答

3

如何只在按鈕上一些JavaScript像這樣

<button type="button" onclick="window.open('http://fake.com/Report/PageFixes/9')">submit</button> 

或者,如果你希望建立網址動態

<button type="button" onclick="window.open('<%= Request.Url.Scheme + "://"+Request.Url.Authority+Request.ApplicationPath %>Report/PageFixes/9')">submit</button> 
6

你並不需要有一個form 。只是下面的HTML代碼添加到您的視圖:

<button type="button" onclick="window.open('<%= Url.Action("PageFixes", "Report", new { id = "9" }) %>', '_blank'); return false;">submit</button> 

我不知道你是怎樣的ID獲得9但我相信它是從你的模型,這樣可以用Model.ID什麼更換"9"

關鍵是生成與Url.Action的網址,然後只使用基本的JavaScript功能window.open打開一個新的瀏覽器窗口中的網址。

+0

工作就像一個魅力爲了我。謝謝! – jason 2012-03-13 15:51:58

+0

感謝您的建議。這對我來說很有用。 Enterprise Blackberry Summary SMAC Report 2015-06-05 19:49:59

0

您仍然可以通過onclick JavaScript函數以舊式的方式進行操作。以下是我做到了,在這裏我需要從模型中的ID傳遞給我新的URL,這是另一種觀點認爲,這是一個文件夾在我的項目中同一目錄分支:

<input type="button" id="@Model.Id" data-id="@Model.Id" onclick="openUrl(this)" data-myurl="/OtherBranch/[email protected]" /> 

function openUrl(e) { 
    var id = e.id; 
    var obj = $("[id='"+id+"']"); 
    var currentUrl = window.location.href; 
    var newBranch = obj.attr('data-myurl'); 
    var newUrl = currentUrl.split('/FirstBranch')[0] + newBranch; 
    window.open(newUrl, "", "height=600,width=400,addressbar=no,menubar=no"); // pop-up window -- should probably ensure no master page is used 
    //window.location.href = newUrl; // <-- total redirect option 
}