andyhutch77 / MvcRazorToPdf

Create pdf documents within an asp .net mvc project by generating your views as normal but returning a PdfActionResult. This converts regular produced razor/html to pdf documents in the browser using the iTextXmlWorker.
125 stars 105 forks source link

Can I use ViewBag instead of passing a model? #30

Closed germanger closed 9 years ago

germanger commented 9 years ago

Can I use ViewBag instead of passing a model?

eg. Instead of this:

            return new PdfActionResult(someModel, (writer, document) =>
            {
                document.SetPageSize(new Rectangle(792f, 612f));
                document.NewPage();
            })
            {
                FileDownloadName = "foo.pdf"
            };

Something like:

            ViewBag.someModel1 = someModel1;
            ViewBag.someModel2 = someModel2;

            return new PdfActionResult(null, (writer, document) =>
            {
                document.SetPageSize(new Rectangle(792f, 612f));
                document.NewPage();
            })
            {
                FileDownloadName = "foo.pdf"
            };

Why? because I don't like the idea of creating a model only for the purpose of transporting two different objects to that view.

andyhutch77 commented 9 years ago

Yes, I believe so as the ViewBag is used for the Title in all the examples. :)

germanger commented 9 years ago

Awesome! works great! Thanks.

Maybe there should be an overload for the PdfActionResult constructor so that it is not needed to pass a null

Instead of this:

        return new PdfActionResult(null, (writer, document) =>
        {
            document.SetPageSize(new Rectangle(792f, 612f));
            document.NewPage();
        })
        {
            FileDownloadName = "foo.pdf"
        };

Just this:

        return new PdfActionResult((writer, document) =>
        {
            document.SetPageSize(new Rectangle(792f, 612f));
            document.NewPage();
        })
        {
            FileDownloadName = "foo.pdf"
        };

I could make a pull request

andyhutch77 commented 9 years ago

Yes, I would probably just add a default parameter and initialise it to null, that way we won't need to create an overload but it will achieve the same effect. :+1:

germanger commented 9 years ago

Ah right, I would do it that way too

germanger commented 9 years ago

Can I use ControllerContext.GeneratePdf with ViewBag instead of passing the model?