1

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.

photo_tom
  • 7,292
  • 14
  • 68
  • 116

2 Answers2

3

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.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • That is similar to what I was thinking. In regular asp.net it is not hard. Documentation on response.filter gives you all you need, but again, in mvc not sure. Also, was not aware you could accept answeres. Still getting to know stackoverflow. But this is a really neat site. Thanks – photo_tom Sep 12 '09 at 10:52
3

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();
}
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133