简体   繁体   English

使用 Asp.Net Core,如何将视图作为新的浏览器选项卡打开,然后稍后再关闭

[英]Using Asp.Net Core, how can I open a view as a new browser tab, and then close it again later

While I want to avoid Pop-ups (due to pop-up blockers and the like), I do want a certain link when clicked to open up the associated View in a new browser tab.虽然我想避免弹出窗口(由于弹出窗口阻止程序等),但我确实希望在单击以在新浏览器选项卡中打开相关视图时获得某个链接。 But then within that view (just a form with some user elements and a submit button), when it's submitted, I need to have it then close the new tab (essentially returning control to the original tab).但是在那个视图中(只是一个带有一些用户元素和一个提交按钮的表单),当它被提交时,我需要让它关闭新选项卡(基本上将控制权返回到原始选项卡)。

I thought I had it solved with adding target="_blank" to the tag in question, and at the end of the controller force it to redirect to a CloseTab view that was nothing more than javascript to call window.close();我想我已经通过将 target="_blank" 添加到有问题的标签来解决它,并在 controller 的末尾强制它重定向到 CloseTab 视图,该视图只不过是 javascript 调用 Z05B8closeC74CBD96FBF244C1A3527。

Unfortunately, while opening the new tab works, when it tries to close the tab after the submit is complete, it fails with "Scripts may close only the windows that were opened by them"不幸的是,虽然打开新选项卡有效,但当它在提交完成后尝试关闭选项卡时,失败并显示“脚本可能只关闭他们打开的 windows”

How can I close the tab that I opened with the target _blank?如何关闭使用目标 _blank 打开的选项卡? Or should I be using some different method (not a popup, so window.open is out) to open the view in a new tab?或者我应该使用一些不同的方法(不是弹出窗口,所以 window.open 已经退出)在新选项卡中打开视图?

My code is below:我的代码如下:

In the calling view (the tag mentioned before):在调用视图中(前面提到的标签):

<a asp-controller="TimePunches" asp-action="PunchIn" target="_blank">Punch In</a>

PunchIn.cshtml: PunchIn.cshtml:

@model TimePunchViewModel

<form asp-action="PunchIn">
    @{ 
        // TODO: user editable fields go here
    }

    <input type="submit" />
</form>

TimePunchController.cs: TimePunchController.cs:

public IActionResult PunchIn()
{
    var model = new TimePunchViewModel();

    return View(model);
}
[HttpPost]
public async Task<IActionResult> PunchIn(TimePunchViewModel model)
{
// TODO: save based on model

// close the tab
    return View("CloseTab");
}

CloseTab.cshtml:关闭选项卡.cshtml:

<body>
    <script type="text/javascript">
        window.close();
    </script>
</body>

You can use ajax submit the form and close window in the success callback function.您可以使用 ajax 提交表单并在成功回调 function 中关闭 window。

PunchIn.cshtml: PunchIn.cshtml:

<form asp-action="PunchIn" method="post">
    @{ 
        // TODO: user editable fields go here
    }
    <input id="submit" type="submit" value="submit" />
</form>

Scripts:脚本:

<script>
    $("#submit").click(function (e) {
        e.preventDefault();
        $.ajax({
            method:"post",
            url: url,
            data: data,
            success: function () {
                window.close();
            }
        })
    })
</script>

Controller: Controller:

[HttpPost]
public void PunchIn(TimePunchViewModel model)
{
    // TODO: save based on model

}

Result:结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core MVC 是否可以在不使用 controller 的情况下在新选项卡中打开视图? - ASP.NET Core MVC is possible to open a view in a new tab without using the controller? 在同一浏览器中打开新标签页时如何关闭标签页? - How can close the tab when open the new tab in same browser? 当我按asp.net超链接时,我想在浏览器中打开新标签页 - I want to open new tab in browser when I press on asp.net hyperlink 如何使用量角器和Chrome浏览器打开新标签页 - How can i open a new tab using protractor and Chrome browser 如何使用 Asp.Net MVC 和 JavaScript 关闭浏览器 window - How to close browser window using Asp.Net MVC and JavaScript 如何在asp.net中检测浏览器和窗口关闭我正在使用Window.onbeforeunload - How to detect the the Browser and window close in asp.net i am using Window.onbeforeunload 运行一些代码后,我想从asp.net背后的代码中关闭浏览器的当前选项卡 - After running a few code I want to close current tab of browser from codebehind asp.net 如何在ASP.NET中的Linkbutton中右键单击“在新选项卡中打开”和“在新窗口中打开”? - How to manage right click “Open in new tab” and “Open in new windows” in Linkbutton in ASP.NET? 关闭浏览器选项卡并再次打开页面后,如何使用 vue-msal 从 AAD 重新获取令牌? - How do I re-acquire a token from AAD using vue-msal after I close the browser tab and open the page again? porthole.js-如何关闭使用window.open()从新标签页打开的标签页 - porthole.js - how can I close a tab that I opened using window.open(), but from the new tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM