12

I am building an mvc app for reporting. I have a page that has a form on it which contains multiple dropdownlist to choose some criteria for a report. I then have an input button to create the report. This button call a new view from the same controller. The new view gets the values from the page where the criteria is chosen from parameters and uses that to populate it's own view model. This is all working fine.

I would like to open the reports in a new window. When I look at the controller, all of the parameters that are supposed to be coming from the selection page are null. I assume I will have to pass these in via the querystring to be picked up by the controller. Is there a way that I can get the values of the dropdownlists from within my viewpage to construct the querystring?

Is this a good way to accomplish what I am trying to do? Would I be better of using an ActionLink instead of an input button? does it make any difference?

I hope this all makes sense. Thanks for any thoughts.

2 Answers 2

38

Just set a target attribute on your form to _blank and it should open the request in a new page/tab depending on the browser being used.

<% using (Html.BeginForm(myAction, myController, FormMethod.Post, new { target = "_blank" })
   { %>
       <%-- ... --%>
<% } %>
Sign up to request clarification or add additional context in comments.

1 Comment

Learning from you that I can set target="_blank" in Html.BeginForm() was like tasting a drop of honey from nowhere!
6

As NickLarsen says...

You could use the target="_blank" attribute of the form element to display the results in a new window.

<form action="/controller/action" method="post" target="_blank">

Or

<% Html.BeginForm("action", "controller", FormMethod.Post, new { target="_blank" }); %>
  //...
<% Html.EndForm(); %>

2 Comments

The BeginForm overload you are using takes route values. Can the target be set in route values? I thought it would have to be set in html attributes. I have not tried what you have written though, just curious.
@NickLarsen You are correct, I typed the wrong overload. I've updated it to use the htmlAttributes object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.