Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a new window using MVC

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.

<% using (Html.BeginForm("PageFixes", "Report", new { id = 9 }, FormMethod.Get, new { onSubmit="window.open()" })) %>
<% { %>
    <%= Html.CSButton(new ButtonViewData() { Text = "Submit" })%>
<% } %>
like image 449
Aaron Salazar Avatar asked Jun 03 '26 16:06

Aaron Salazar


1 Answers

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

<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 9 for the ID but I assume it is from your model so you could replace the "9" with Model.ID or something.

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

like image 159
Kelsey Avatar answered Jun 05 '26 07:06

Kelsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!