views:

59

answers:

1

Thought I would pose this to the StackOverflow crowd since I've run out of ideas.

I have a request from users that once a button is clicked, a PDF form is generated which they would like to automatically view, and once they close the PDF the page will already be a "Final page", not the page they clicked the button from.

In my pre-final page with the button, the controller calls:

return File(finalForm, "application/pdf", Server.HtmlEncode(finalForm));

But this has now passed the control to the client, I can't route to a different View.

Any clever ideas on how I can also display a new view?

+1  A: 

I've broken this down into two separate actions on the Home Controller, using the FinalPage action as the view you get redirected to and the GetFile action as the one to return the file itself.

Controller

    public ActionResult GetFile()
    {
        return File(@"path to pdf.pdf", "application/pdf");
    }

    public ActionResult FinalPage()
    {
        return View();
    }

View

<script>

    function showfile() {
        window.open('<%= Url.Action("GetFile", "Home")%>')
    }

</script>

<%= Html.ActionLink("click", "FinalPage", "Home", null, new { onclick = "return showfile();" }) %>

This will open up a new window and get the file returned to display, but also move the other browser window onto the final page on the same click.

Hope this helps.

Edit

Updated to run off a submit button as per comment ... in answer to the comment, yes you can do it off a submit button :-)

<script>

    function showfile() {
        window.open('<%= Url.Action("GetFile", "Home")%>')
    }

</script>

<% using(Html.BeginForm("FinalPage", "Home")) { %>

    <input type="Submit" value="click" onclick="return showfile();" />

<% } %>

Hope this helps :-)

WestDiscGolf
This is great, I really appreciate the example, very clear!
Mark Kadlec
I currently have a button (<input type="submit"...) instead of a link, is this still possible?
Mark Kadlec
Yes, please see the update to the answer. Hope it helps :-)
WestDiscGolf
WestDiscGolf, you rock. I just implemented your suggestion for the submit and it works great! I actually see a lot of commercial site not using the rerouting after pdf generation, so hopefully this will help some other developers. Thanks for your time
Mark Kadlec
No worries, glad I could help out :-)
WestDiscGolf