How to get ASP.NET MVC to open a new window?
I’m trying to put together a MVC app that isn’t a typical create a record and present the record to the user.
I’m finding some things that aren’t friendly in MVC as they are in Web Forms. My view has two dropdowns, a textbox, and a submit button. In one of the dropdowns I have to prepopulate it with codes and description. That part is done.
Next, I have the user en开发者_如何学JAVAter text into the textbox. They click on a Find button. Find will populate the 2nd dropdown. Without viewstate, the code is a bit different but possible. Next, the user clicks on the submit button. Here is the tricky part. I need input from the view to generate a PDF file, then open a new browser window or tab to display the PDF.
I also want to delete the PDF from the server before presenting it. I can delete the PDF before presenting it in web forms. I found Actionlink can open a new window, but Actionlink doesn’t push the input on the view to the controller. A standard form submit button does, but a submit button doesn’t open a new window. A controller cannot open a view in a new window either.
How do I get the users input and push that input to the controller, and then display the PDF generated into a new browser window? On top of that, I need to delete the PDF off the server.
Simple create an Action that returns your Pdf stream as a FileResult
If you are generating your pdf to a memory stream: (I recommend this):
public ActionResult DownloadPdf()
{
// you need some code here to generate the pdf to the memory stream.
return File(stream, "application/pdf", "DownloadName.pdf");
}
Or if you prefer, use the filepath directly :
public ActionResult DownloadPdf()
{
// get pdf filepath
var path = "Chap0101.pdf";
return File(path, "application/pdf");
}
Then in your html code you can use something like this to open in a new window.
@Html.ActionLink("Download pdf in new window", "DownloadPdf", "ControllerName", null, new { target = "_blank" })
You might return a view with some javascript that first opens a new window and then refreshes to another view. If no javascript is active, the view wouldn't reload and a link would be there to open manually. If you create the PDF in memory, there is nothing you need to delete, just stream the in memory PDF to the client. I'm new to MVC myself, this is just an idea.
精彩评论