0

I'm working on the MVC app where I've to generate the report in the form of HTML page. If the user click the print icon, I've to show the HTML page to user. If the user click on email icon, I've to send email with same HTML page attachment. I'm trying to find a way where I can use the same code to generate the HTML in both cases of email and print. Please provide your suggestions.

gmail user
  • 2,753
  • 4
  • 33
  • 42

2 Answers2

1

What you really want and did not know how to formulate is Render view to string. Then you can do whatever you want with the contents of that string.

Start here Render a view as a string

but this subject continues in many other questions too (or you can Google it) and you will discover much more information.

Community
  • 1
  • 1
mare
  • 13,033
  • 24
  • 102
  • 191
0

Your Controller has to decide what to do.

  1. User clicked print. Controller action: collect data, prepare the View, display as HTML page
  2. User clicked email. Controller action: collect data, prepare the View, call on an email-function and use the output of the HTML page as an attachment.
Alec
  • 9,000
  • 9
  • 39
  • 43
  • but how to reuse the same view? – gmail user May 18 '10 at 20:14
  • In the first scenario you send it to the browser; in the second scenario you add the HTML to a file, or create the attachment on the fly from within the email method that gets called in your Controller. If the HTML is the same for both scenarios, you can use the same HTML, the difference is what you do with it. – Alec May 18 '10 at 20:29
  • HTML is same. I'm not where to put the code to generate the HTML. Because HTML is usually in the VIEW and I'm not sure how to grab it from view and put in the file – gmail user May 18 '10 at 20:46
  • Usually your Controller has a method that displays the View (the HTML in this case). You can either display it in the browser, store it in a variable or write it to a file. The last two can both be used for the attachment in the email. It really depends on your code for both displaying the view and sending the email on how to do this exactly. – Alec May 18 '10 at 21:04