Opening a new window using MVC

15,678

Solution 1

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.

Solution 2

How about just some javascript on a button like so

<button type="button" onclick="window.open('http://fake.com/Report/PageFixes/9')">submit</button>

Or if you would prefer to build the URL dynamically

<button type="button" onclick="window.open('<%= Request.Url.Scheme + "://"+Request.Url.Authority+Request.ApplicationPath %>Report/PageFixes/9')">submit</button>
Share:
15,678
Aaron Salazar
Author by

Aaron Salazar

C# .NET Developer

Updated on June 08, 2022

Comments

  • Aaron Salazar
    Aaron Salazar almost 2 years

    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" })%>
    <% } %>
    
  • jason
    jason about 12 years
    Worked like a charm for me. Thanks!
  • Kremena Lalova
    Kremena Lalova almost 9 years
    Thanks for the advice. This is what worked for me. <a href="" onclick="window.open('@Url.Action("BlackBerrySummarySMACExpo‌​rt", new { departmentId = @ViewBag.DeptId })', '_blank', 'menubar=no'); return false;">Enterprise Blackberry Summary SMAC Report</a>