4

Off the bat: I am new to using asp.net mvc 4.

I am have a action that creates a excel file and then converts it to PDF.

From View

@Html.ActionLink("Generate Invoice", "genInvoice", new { id = item.invoiceID }) |

Action:

public ActionResult genInvoice(int id = 0)
    {
        var invoiceItems = from k in db.InvoiceItems
                           where k.invoiceID == id
                           select k;

        string invoiceClient = (from kk in db.Invoices
                                where kk.invoiceID == id
                                select kk.clientName).Single();


        invoiceClient = invoiceClient + "_" + DateTime.Now.ToString("ddd dd MMM yyyy hhTmm");
        string websitePath = Request.PhysicalApplicationPath;
        string pathName = websitePath + "\\" + invoiceClient ;
        generateInvoice(invoiceItems, pathName + ".xlsx", id);
        convertToPDF(pathName, invoiceClient);

//Response.AppendHeader("Content-Disposition", "attachment");

          var viewModel = new InvoiceItemAdd();
          viewModel.Invoices = db.Invoices
              .Include(i => i.InvoiceItems)
              .OrderBy(i => i.invoiceID);
        return View("Index", viewModel);
        //return RedirectToAction("Index",viewModel);


    }

Now I want to to eventually download the PDF file and then return to the index view. It goes to the Index view prints the html etc etc but then the window stays as a white screen with the url: /Invoice/genInvoice/1

Any idea how I can go about doing this? (Going back to the Index view after PDF generation, also downloading it)

2
  • Why don't you move the whole viewModel part to the index controller? Commented Nov 30, 2012 at 10:04
  • var viewModel = new InvoiceItemAdd(); viewModel.Invoices = db.Invoices .Include(i => i.InvoiceItems) .OrderBy(i => i.invoiceID); that part? It is in there, but when I call it like that the view says the model is empty Commented Nov 30, 2012 at 10:41

1 Answer 1

1

I am sorry, I fixed the white screen problem. While attempting to do the PDF download

//Response.AppendHeader("Content-Disposition", "inline; filename="+invoiceClient+".pdf");
            //Return File(output, "application/pdf");
            //Response.Flush();

            //Response.End();

Response.End() was not commented out and that stopped it I guess.

Now the problem is how to open the PDF in a separate tab and return to index in the current with the above code.

EDIT: Decided the file can just be downloaded.

public FileResult genInvoice(int id = 0)
{
//More code
Response.AppendHeader("Content-Disposition", "attachment; filename="+pathName+".pdf");
return File(websitePath + "\\" + invoiceClient + ".pdf", "application/pdf");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Try changing it from an ActionResult to a FileStreamResult or FileResult, that will make sure the file gets downloaded without returning a white screen.
Ok thanks, that opens the PDF which is good. But I want it to open the pdf in a separate tab and the go/stay on the index page in the current one
You could add a target="_blank" to the link to achieve that. But I expect the file will download and you will not be redirected to another (blank) page with the above code

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.