2010-08-31 141 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 
}