1

I want to use the Title Dynamically in my MVC application. Because I am not returning the View instead of that I am returning the PDF report as:

  public ActionResult PrintInvoice(long ID)
    {
        var data = db.Documents.Where(x => x.InvoiceNumber == ID);
        ReportDocument rd = new ReportDocument();
        rd.ParameterFields["DocumentID"].CurrentValues.IsNoValue = true;
        Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);
        return new FileStreamResult(stream, "application/pdf");               //For Showing PDF in Browser itself

    }

and I a getting Page Title in the Form like,

Page Title shown by Default

But I want to Change the Page title here. What should I do for changing the page title Dynamically from the MVC's action itself?

Rahul
  • 2,309
  • 6
  • 33
  • 60
  • @meilke in my case there is different Scenario. I want to put the Title on the Browser's tab into my action itself. – Rahul Jan 29 '14 at 09:57
  • My mistake: http://stackoverflow.com/questions/15862683/asp-net-mvc-filestreamresult-filedownloadname-is-not-used. – meilke Jan 29 '14 at 10:00
  • @meilke am am not worried about the filename at the time of Downloading. I just want to show the Title on the Web Page opened in Browser. – Rahul Jan 29 '14 at 10:02
  • What are you worried about then? – meilke Jan 29 '14 at 10:03
  • @meilke Please see the Image I have attached with my question for getting the point of Title in my question – Rahul Jan 29 '14 at 10:06

3 Answers3

1

I don't think you actually can. You are not sending a html/text response back to the browser, but a application/pdf response. In most cases the browser wont even open a tab. (It opens and closes quickly). The title of the page is part of the html/text response, but since you have none..

Maarten Kieft
  • 6,806
  • 3
  • 29
  • 34
0

There is a little trick to get this requirement done. You can you iFrame in the page and iFrame can handle file download.

Main Action -

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

Main View -

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<iframe src="@Url.Action("FileDownload")"></iframe>

FileDownload Action -

public ActionResult FileDownload()
{
    return File(System.IO.File.ReadAllBytes(Server.MapPath("~/rami.pdf")), System.Net.Mime.MediaTypeNames.Application.Octet, "rami.pdf");
}

With little styles to iFrame, you can completely merge it on the main view. Here you can set the Title for main view, and the iframe action will trigger download.

For dynamic downloads, you might need to pass some information to iframe src probably a querystring, which will be used by FileDownload action and will prompt download for that corresponding file.

ramiramilu
  • 17,044
  • 6
  • 49
  • 66
0

As others have pointed out, you're not returning a document, you're returning the file.

I'd suggest using an ajax called to actually perform the download in the background. For more information see this question and answer(s). In this case it is an Excel document, but that should make no difference to your use-case.

Community
  • 1
  • 1
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128