[英]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.