BlackICE
BlackICE

Reputation: 8926

Should I write from Controller, Code Behind, or Helper in MVC?

I've seen all the questions and answers around not having code-behind for a view, however I have a case where I need complex logic to generate the presentation (view) layer. I have to output a PDF file based on data obtained from db. Where is the best place to generate this PDF and write to the response stream? Doing response.write from the controller feels very wrong to me, but I would like responses to this, and to using a code-behind file for the view to generate the PDF. I suppose I could encapsulate the data in a viewmodel class and pass that to a Helper method to generate the output as well, what would be considered best practice in this case, specifically having a lot of logic around creating the PDF?

Upvotes: 0

Views: 608

Answers (2)

Francesco Abbruzzese
Francesco Abbruzzese

Reputation: 4149

The better way to do it is by defining an ActionResult specific for outputting pdf files. This way you can reuse the code easily also in other applications

Upvotes: 1

Mattias Jakobsson
Mattias Jakobsson

Reputation: 8237

I would create a ActionResult class for this and return that from the controller. The ActionResult class is responsible for writing stuff to the output stream.

Upvotes: 3

Related Questions