使用MVC打开一个新窗口

时间:2010-08-31 00:43:06

标签: c# javascript asp.net asp.net-mvc

基本上我想要一个按钮,在包含我的http://fake.com/Report/PageFixes...页面的新窗口中打开页面。目前我在表单中有一个按钮但是如果有更好的方法可以做到这一点,那么我也是这样做的。

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

3 个答案:

答案 0 :(得分:6)

您不需要form。只需将以下HTML添加到您的视图中:

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

我不确定你是如何获得9的{​​{1}}但我认为它来自你的模型,因此您可以将ID替换为"9"或东西。

关键是使用Model.ID生成网址,然后只使用基本的javascript函数Url.Action在新的浏览器窗口中打开网址。

答案 1 :(得分:3)

如此按钮上的一些javascript怎么样

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

或者,如果您希望动态构建URL

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

答案 2 :(得分:0)

你仍然可以通过onclick JavaScript函数以老式方式完成它。这是我如何做到的,我需要将ID从模型传递到我的新URL,这是另一个View,它位于我项目中同一目录分支的文件夹中:

<input type="button" id="@Model.Id" data-id="@Model.Id" onclick="openUrl(this)" data-myurl="/OtherBranch/Detail?id=@Model.Id" />

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
}