views:

184

answers:

2

I need to render a view to send as an email using Asp.Net MVC 2.

Using the new Html.Partial method it is easy to render a view to a string and then send it as an email as long as you do it from inside a controller or a view (where you can access the html helper or the controller context)

I need to be able to send a delayed email using a background service. I want to render them in the same way as I would in a controller but I can't access the Html Helper or the controller context.

I tried to make my own method using the ViewPage class and calling its render method myslef passing in a stringwriter. Problem is that I don't have a view context for the html helper, so I can only render views that don't use the html helper or the url helper.

Any Ideas.

Thanks

A: 
ThreadPool.RegisterWaitForSingleObject(new ManualResetEvent(false), 
    (state, timeout) => 
    {
        var context = (HttpContext)state;
        // Use the context here
    }, HttpContext, TimeSpan.FromSeconds(10), true);
Darin Dimitrov
Getting the http context isnt the problem. I need the controller context to create an html helper object or to try to render a ViewPage on my own
Sruly
The controller context will not help you instantiate an HtmlHelper as it requires a `ViewContext` which is not available in the controller. You could use a block renderer from MvcContrib (http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/).
Darin Dimitrov
If I have the controller context I can create a view context. I have done this before
Sruly
+1  A: 

MVCContrib has something that may help you

http://github.com/mvccontrib/MvcContrib/blob/master/src/MVCContrib/Services/EmailTemplateService.cs

jfar