tags:

views:

52

answers:

1

I have an HtmlHelper extension that currently returns a string using a string builder and a fair amount of complex logic. I now want to add something extra to it that is taken from a render partial call, something like this ...

public static string MyHelper(this HtmlHelper helper)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("Hi There");
    builder.Append(RenderPartial("MyPartialView"));
    builder.Append("Bye!");
    return builder.ToString();
}

Now of course RenderPartial renders directly to the response so this doesn;t work and I've tried several solutions for rendering partials to strings but the all seem to fall over one I use the HtmlHelper within that partial.

Is this possible?

+1  A: 

You shouldn't be calling partials from a helper. Helpers "help" your views, and not much else. Check out the RenderAction method from MVCContrib (if you need it now) or MVC v2 (if you can wait a few more months). You'd be able to pass your model to a standard controller action and get back a partial result.

Jarrett Meyer
+1 yeah I wouldn't want to render a partial from a helper.
griegs
In some circumstances yes (probably including this one), but I try to keep my HTML in HTML like files where possible and use Html helpers to do the logic around how to combine them. This way I still get nice editor features in my html. However not using render partial is exactly what I've done in this case.
Chris Meek