javascript 使用MVC打开一个新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3605264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:41:34  来源:igfitidea点击:

Opening a new window using MVC

c#javascriptasp.netasp.net-mvc

提问by Aaron Salazar

Basically I want a button that opens a page in a new window containing my http://fake.com/Report/PageFixes...page. Currently I have a button in a form but if there is a better way to do it then I'm up for that too.

基本上我想要一个按钮,可以在包含我的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" })%>
<% } %>

回答by Kelsey

You don't need to have a form. Just add the following HTML to your view:

你不需要有一个form. 只需将以下 HTML 添加到您的视图中:

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

I wasn't sure how you were getting the 9for the IDbut I assume it is from your model so you could replace the "9"with Model.IDor something.

我不确定您是如何获得9for 的,ID但我认为它来自您的模型,因此您可以替换"9"withModel.ID或其他东西。

The key is generating the url with Url.Actionand then just using the basic javascript function window.opento open the url in a new browser window.

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

回答by Chmod

How about just some javascript on a button like so

像这样的按钮上的一些javascript怎么样

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

Or if you would prefer to build the URL dynamically

或者,如果您更喜欢动态构建 URL

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

回答by vapcguy

You could still do it the old-fashioned way with an onclick JavaScript function. Here's how I did it, where I needed to pass the ID from the Model to my new URL, which was to another View that was in a folder on the same directory branch in my project:

您仍然可以使用 onclick JavaScript 函数以老式方式完成此操作。这是我的操作方法,我需要将模型中的 ID 传递到我的新 URL,该 URL 是另一个视图,该 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
}