views:

67

answers:

2

What's the best way to create printable letters from an MVC application? I'm looking for sort of a mail merge thing from my app that prints a form letter with various values filled in.

In ASP.NET, I previously did this by creating an HTML document and displaying it as application/msword, but I did that with code-behind, which isn't an (easy) option in MVC, and I don't know if that's the best method or not.

Note that this is an internal application, so it can be assumed everyone has Word on their computer. With that said, it would be nice to bypass Word, but I could go either way. The simpler the better. Any ideas/methods welcome.

+1  A: 

Since this is just HTML with the ContentType set to application/msword I can't see any reason why you would want to use code-behind.

A standard MVC view with a typical HTML template peppered with appropriate <%=...> where view data needs to be inserted would seem to be the sensible approach. Even where you might want to loop.

BTW, why isn't code-behind an easy option?

AnthonyWJones
Wouldn't this method leave the printing up to the browser, which adds all the artifacts to the header and footer?
gfrizzle
"Easy option" is probably the wrong way of putting it. MVC sort of "discourages" code-behind, so I was trying to stay in that vein, but if that's the right tool for the job, I've got no problem going there.
gfrizzle
@gfizzle: if you are setting ContentType = "application/word" as you stated then the browser will not be handling the content, Word will.
AnthonyWJones
For the most part views should not need code-behind because most oftent code-behind denotes logic that is chosing/munging data. If your code-behind is entirely related to the presentation of the date provided by the controller then its good even desirable.
AnthonyWJones
A: 

In your controller:

return Content(contentGoesHere, "application/msword");
John Sheehan