MVC按钮单击在新窗口中打开

Kan*_*aya 4 asp.net-mvc razor

嗨,我需要在新的窗口中单击此按钮打开我的视图,但是它将在同一窗口中打开。这是我使用的语法。

<button  id="btnPrintReport" onclick="location.href='@Url.Action("LoadReports", "DashBoard", new { target="_blank" })'" >Print</button>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?谢谢。

Ari*_*jee 5

尝试 window.open在新标签页中打开链接

onclick="window.open('@Url.Action("LoadReports", "DashBoard", new { target="_blank" })')"
Run Code Online (Sandbox Code Playgroud)

演示


Mar*_*rco 1

据我所知,<button>不会与target. 元素也是如此input type="button" ... />

您需要使用锚点并使用 css 作为按钮对其进行样式设置

<a href=@Url.Action("LoadReports", "DashBoard") id="aPrintReport" target="_blank" class="button">Print</a>

<!--taken from http://stackoverflow.com/questions/2187008/styling-an-anchor-tag-to-look-like-a-submit-button-->
.button {
    text-decoration: none; font: menu;
    display: inline-block; padding: 2px 8px;
    background: ButtonFace; color: ButtonText;
    border-style: solid; border-width: 2px;
    border-color: ButtonHighlight ButtonShadow ButtonShadow ButtonHighlight;
}
.button:active {
    border-color: ButtonShadow ButtonHighlight ButtonHighlight ButtonShadow;
}
Run Code Online (Sandbox Code Playgroud)