<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: RenderPartial to String in ASP.NET MVC</title>
	<atom:link href="http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/</link>
	<description></description>
	<lastBuildDate>Wed, 26 Jan 2011 18:41:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: Wessel</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1340</link>
		<dc:creator>Wessel</dc:creator>
		<pubDate>Fri, 20 Aug 2010 09:36:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1340</guid>
		<description>The solution is very simple.

Create a new Viewclass based on the WebFormView, and use that as the ViewEnging for rendering (by returning this View in the CreatePartialView and CreateView methods of a custom WebFormViewEngine).

Override the Render method in your new Viewclass and create a new TextWriter.
Store the viewContent.Writer and set the viewContent.Writer to the newly created TextWriter.
Call MyBase.Render(viewContext,writer).
The writer is ignored, but you just changed the writer of the viewContext so any output goes there and not to the Response!

Restore the viewContext.Writer.

You can now use the content of the newly created TextWriter and do anything you like.
Finally write the changed content to the old viewContent.Writer (which writes to Response).

Performance does not degrade as far as I can tell and this works for partialrendering as well as for normal pages.
Basically for all view rendered output.

Example:

Imports System.IO

Public Class CustomRenderView
    Inherits WebFormView

    Public Sub New(ByVal viewPath As String)
        MyBase.New(viewPath)
    End Sub

    Public Sub New(ByVal viewPath As String, ByVal masterPath As String)
        MyBase.New(viewPath, masterPath)
    End Sub

    Public Overrides Sub Render(ByVal viewContext As System.Web.Mvc.ViewContext, ByVal writer As System.IO.TextWriter)
        Dim newWriter As TextWriter = New StringWriter()

        Dim oldWriter As TextWriter = viewContext.Writer
        viewContext.Writer = newWriter

        MyBase.Render(viewContext, newWriter) &#039;use newWriter in case MS changes its mind to ignore it...
        viewContext.Writer = oldWriter

        Dim pageSource As String = newWriter.ToString()
        &#039;do something with the generated output in pageSource

        &#039;write changed output to the original viewContext.Writer (goes to Response object)
        oldWriter.Write(pageSource)
    End Sub

End Class

That&#039;s all folks!</description>
		<content:encoded><![CDATA[<p>The solution is very simple.</p>
<p>Create a new Viewclass based on the WebFormView, and use that as the ViewEnging for rendering (by returning this View in the CreatePartialView and CreateView methods of a custom WebFormViewEngine).</p>
<p>Override the Render method in your new Viewclass and create a new TextWriter.<br />
Store the viewContent.Writer and set the viewContent.Writer to the newly created TextWriter.<br />
Call MyBase.Render(viewContext,writer).<br />
The writer is ignored, but you just changed the writer of the viewContext so any output goes there and not to the Response!</p>
<p>Restore the viewContext.Writer.</p>
<p>You can now use the content of the newly created TextWriter and do anything you like.<br />
Finally write the changed content to the old viewContent.Writer (which writes to Response).</p>
<p>Performance does not degrade as far as I can tell and this works for partialrendering as well as for normal pages.<br />
Basically for all view rendered output.</p>
<p>Example:</p>
<p>Imports System.IO</p>
<p>Public Class CustomRenderView<br />
    Inherits WebFormView</p>
<p>    Public Sub New(ByVal viewPath As String)<br />
        MyBase.New(viewPath)<br />
    End Sub</p>
<p>    Public Sub New(ByVal viewPath As String, ByVal masterPath As String)<br />
        MyBase.New(viewPath, masterPath)<br />
    End Sub</p>
<p>    Public Overrides Sub Render(ByVal viewContext As System.Web.Mvc.ViewContext, ByVal writer As System.IO.TextWriter)<br />
        Dim newWriter As TextWriter = New StringWriter()</p>
<p>        Dim oldWriter As TextWriter = viewContext.Writer<br />
        viewContext.Writer = newWriter</p>
<p>        MyBase.Render(viewContext, newWriter) &#8216;use newWriter in case MS changes its mind to ignore it&#8230;<br />
        viewContext.Writer = oldWriter</p>
<p>        Dim pageSource As String = newWriter.ToString()<br />
        &#8216;do something with the generated output in pageSource</p>
<p>        &#8216;write changed output to the original viewContext.Writer (goes to Response object)<br />
        oldWriter.Write(pageSource)<br />
    End Sub</p>
<p>End Class</p>
<p>That&#8217;s all folks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Craft</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1299</link>
		<dc:creator>Kevin Craft</dc:creator>
		<pubDate>Sat, 15 May 2010 17:11:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1299</guid>
		<description>I wrote a post that solves the same problem in a slightly different way. I dug into the ASP.NET MVC source and used the method they use. The Html and Ajax helpers work properly. You also don&#039;t have to provide the full path to the partial view&#039;s filename. You can use it just like the &quot;PartialView&quot; function in the controller. Check it out at &lt;a href=&quot;http://craftycode.wordpress.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/&quot; rel=&quot;nofollow&quot;&gt;http://craftycode.wordpress.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>I wrote a post that solves the same problem in a slightly different way. I dug into the ASP.NET MVC source and used the method they use. The Html and Ajax helpers work properly. You also don&#8217;t have to provide the full path to the partial view&#8217;s filename. You can use it just like the &#8220;PartialView&#8221; function in the controller. Check it out at <a href="http://craftycode.wordpress.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/" rel="nofollow">http://craftycode.wordpress.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1284</link>
		<dc:creator>Joshua</dc:creator>
		<pubDate>Thu, 25 Mar 2010 15:24:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1284</guid>
		<description>I have implemented this solution, however, I am curious how we retrieve the data in the partial view?

Currently, I am casting the ViewData Objects in the partial view (not using a view model)



Unfortunately, ViewData seems to be empty when rendering.. No matter what I pass into RenderPartialToString as the viewData...

NE thoughts?</description>
		<content:encoded><![CDATA[<p>I have implemented this solution, however, I am curious how we retrieve the data in the partial view?</p>
<p>Currently, I am casting the ViewData Objects in the partial view (not using a view model)</p>
<p>Unfortunately, ViewData seems to be empty when rendering.. No matter what I pass into RenderPartialToString as the viewData&#8230;</p>
<p>NE thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bobasoft</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1228</link>
		<dc:creator>Bobasoft</dc:creator>
		<pubDate>Sat, 09 Jan 2010 10:23:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1228</guid>
		<description>when use ajax helper in partial view - will be crash. 

http://msug.vn.ua/blogs/bobasoft/archive/2010/01/07/render-partialview-to-string-asp-net-mvc.aspx - nicer solution</description>
		<content:encoded><![CDATA[<p>when use ajax helper in partial view &#8211; will be crash. </p>
<p><a href="http://msug.vn.ua/blogs/bobasoft/archive/2010/01/07/render-partialview-to-string-asp-net-mvc.aspx" rel="nofollow">http://msug.vn.ua/blogs/bobasoft/archive/2010/01/07/render-partialview-to-string-asp-net-mvc.aspx</a> &#8211; nicer solution</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary Davis</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1207</link>
		<dc:creator>Gary Davis</dc:creator>
		<pubDate>Fri, 04 Dec 2009 16:37:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1207</guid>
		<description>Based on what I read in your reference, I made these changes to solve the trap:

&lt;code&gt;
public static string RenderPartialToString(this HtmlHelper htmlHelper, string controlName, object viewData)
{
  // Added Url to allow ascx to use Url.Action()
  var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  // Added ViewContext to allow ascx to use Html helpers
  var vp = new ViewPage { ViewData = vd, ViewContext = htmlHelper.ViewContext, Url = urlHelper};
&lt;/code&gt;

Note this is an Html helper called with Html.RenderPartialToString(). If not, use these changes:

&lt;code&gt;
  var vc = new ViewContext();
  // Added Url to allow ascx to use Url.Action()
  var urlHelper = new UrlHelper(vc.RequestContext);
  // Added ViewContext to allow ascx to use Html helpers
  var vp = new ViewPage { ViewData = vd, ViewContext = vc, Url = urlHelper };
&lt;/code&gt;

Gary</description>
		<content:encoded><![CDATA[<p>Based on what I read in your reference, I made these changes to solve the trap:</p>
<p><code><br />
public static string RenderPartialToString(this HtmlHelper htmlHelper, string controlName, object viewData)<br />
{<br />
  // Added Url to allow ascx to use Url.Action()<br />
  var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);<br />
  // Added ViewContext to allow ascx to use Html helpers<br />
  var vp = new ViewPage { ViewData = vd, ViewContext = htmlHelper.ViewContext, Url = urlHelper};<br />
</code></p>
<p>Note this is an Html helper called with Html.RenderPartialToString(). If not, use these changes:</p>
<p><code><br />
  var vc = new ViewContext();<br />
  // Added Url to allow ascx to use Url.Action()<br />
  var urlHelper = new UrlHelper(vc.RequestContext);<br />
  // Added ViewContext to allow ascx to use Html helpers<br />
  var vp = new ViewPage { ViewData = vd, ViewContext = vc, Url = urlHelper };<br />
</code></p>
<p>Gary</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary Davis</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1206</link>
		<dc:creator>Gary Davis</dc:creator>
		<pubDate>Fri, 04 Dec 2009 16:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1206</guid>
		<description>This worked nicely except when the ascx control used an Html helper (). Html was null causing a Null-ref trap. 

The trap did not occur if the control was referenced by Html.RenderPartial() instead of RenderPartialToString().

Gary</description>
		<content:encoded><![CDATA[<p>This worked nicely except when the ascx control used an Html helper (). Html was null causing a Null-ref trap. </p>
<p>The trap did not occur if the control was referenced by Html.RenderPartial() instead of RenderPartialToString().</p>
<p>Gary</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1122</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Wed, 28 Oct 2009 05:01:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1122</guid>
		<description>When I tried to use this code I had to change this line:
&lt;code&gt;
ViewDataDictionary vd = new ViewDataDictionary(viewData);
&lt;/code&gt;

To:
&lt;code&gt;
ViewPage vp = new ViewPage { ViewData = (ViewDataDictionary)viewData };
&lt;/code&gt;

Not sure why but when I created the ViewDataDictionary object with the constructor I was losing all of viewData&#039;s parameters.</description>
		<content:encoded><![CDATA[<p>When I tried to use this code I had to change this line:<br />
<code><br />
ViewDataDictionary vd = new ViewDataDictionary(viewData);<br />
</code></p>
<p>To:<br />
<code><br />
ViewPage vp = new ViewPage { ViewData = (ViewDataDictionary)viewData };<br />
</code></p>
<p>Not sure why but when I created the ViewDataDictionary object with the constructor I was losing all of viewData&#8217;s parameters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1011</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Tue, 13 Oct 2009 15:01:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1011</guid>
		<description>Hey Dan,

As far as I know, your point is quite valid. If you want to render an entire view to a string, the previous post will work and this one will not.

However, the idea behind RenderPartialToString, is geared at just that, rendering a partial view to a string.</description>
		<content:encoded><![CDATA[<p>Hey Dan,</p>
<p>As far as I know, your point is quite valid. If you want to render an entire view to a string, the previous post will work and this one will not.</p>
<p>However, the idea behind RenderPartialToString, is geared at just that, rendering a partial view to a string.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Atkinson</title>
		<link>http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/comment-page-1/#comment-1010</link>
		<dc:creator>Dan Atkinson</dc:creator>
		<pubDate>Tue, 13 Oct 2009 11:23:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.brightmix.com/?p=2894#comment-1010</guid>
		<description>You link from your last blog post, but this isn&#039;t the same as rendering a view to a string.

This is just rendering a partial as views don&#039;t extend Control.

So, if you wanted to actually generate an entire view into a string, you&#039;d still need to use the old method. That or rework your views to be partials instead...</description>
		<content:encoded><![CDATA[<p>You link from your last blog post, but this isn&#8217;t the same as rendering a view to a string.</p>
<p>This is just rendering a partial as views don&#8217;t extend Control.</p>
<p>So, if you wanted to actually generate an entire view into a string, you&#8217;d still need to use the old method. That or rework your views to be partials instead&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
