5

Hi I need to open my view in a new window on this button click but it opens in the same window. This is the syntax I'm using.

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

Have I missed anything? Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

6

Try window.open to open link in a new tab

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

DEMO

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
1

As far as I know, <button> won't work with target. Same goes for input type="button" ... /> elements:

You need to use an anchor and style it with css as a button

<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;
}
Marco
  • 22,856
  • 9
  • 75
  • 124
  • Hi, What changes required to make the above if the action is post form action? AFAIK Url.Action is GET only. – AKS Jul 04 '16 at 11:54