How to Email Screen in ASP.Net/MVC

I have an application that is made up of 10+ related ascx files that we use to display our data on the users browser using the Html.RenderPartial helper.

I need to email the data that is a duplicate of what is on screen. I would like to be able to get the generated html without having to do a screen scrape.

Are there any suggestions as to how to do this? I’m just trying to keep from duplicating the work.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ has a good solution for rendering a View to a string so you can send it in email.

/// Static Method to render string - put somewhere of your choosing
public static string RenderPartialToString(string controlName, object viewData)
{
     ViewDataDictionary vd = new ViewDataDictionary(viewData);
     ViewPage vp = new ViewPage { ViewData = vd };
     Control control = vp.LoadControl(controlName);

     vp.Controls.Add(control);

     StringBuilder sb = new StringBuilder();
     using (StringWriter sw = new StringWriter(sb))
     {
         using (HtmlTextWriter tw = new HtmlTextWriter(sw))
         {
             vp.RenderControl(tw);
         }
     }

     return sb.ToString();
}

Method 2

In regular asp.net you can override the Render() and provide your own HtmlWriter to intercept the rendered html before copying it to the HtmlWriter that was passed in.

I don’t know off the top of my head how you’d intercept this in MVC, but I’m sure you’ll be able to do it – especially if you make a new HttpModule in the pipeline to pre-post process the output stream.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x