<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET MVC</title><link>http://forums.asp.net/1146.aspx</link><description>Discussions regarding Model-View-Controller (MVC) support in ASP.NET.  &lt;a href="http://forums.asp.net/1215.aspx"&gt;T4MVC subforum&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2400036.aspx</link><pubDate>Wed, 04 Jun 2008 08:19:35 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2400036</guid><dc:creator>mikehadlow</dc:creator><author>mikehadlow</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2400036.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2400036</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;I&amp;#39;ve just written about doing this with CTP 3 by using the MVCContrib BlockRenderer class:&lt;/p&gt;
&lt;p&gt;http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view.html&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s a snippet:&lt;/p&gt;
&lt;pre&gt;           // pass the current controller context to orderController&lt;br /&gt;           orderController.ControllerContext = ControllerContext;&lt;br /&gt;&lt;br /&gt;           // create a new BlockRenderer&lt;br /&gt;           var blockRenderer = new BlockRenderer(HttpContext);&lt;br /&gt;&lt;br /&gt;           // execute the Item action&lt;br /&gt;           var viewResult = (ViewResult) orderController.Item(1);&lt;br /&gt;&lt;br /&gt;           // change the master page name&lt;br /&gt;           viewResult.MasterName = &amp;quot;Print&amp;quot;;&lt;br /&gt;&lt;br /&gt;           // we have to set the controller route value to the name of the controller we want to execute&lt;br /&gt;           // because the ViewLocator class uses this to find the view&lt;br /&gt;           this.RouteData.Values[&amp;quot;controller&amp;quot;] = &amp;quot;Order&amp;quot;;&lt;br /&gt;&lt;br /&gt;           string result = blockRenderer.Capture(() =&amp;gt; viewResult.ExecuteResult(ControllerContext));&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;UPDATE:&lt;/p&gt;
&lt;p&gt;This technique has problems, Here&amp;#39;s a better one:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html"&gt;http://mikehadlow.blogspot.com/2008/06/mvc-framework-capturing-output-of-view_05.html&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2086731.aspx</link><pubDate>Wed, 02 Jan 2008 21:34:44 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2086731</guid><dc:creator>Tom Schreck</dc:creator><author>Tom Schreck</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2086731.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2086731</wfw:commentRss><description>&lt;p&gt;Ah, yes.&amp;nbsp; Partial views is what I&amp;#39;m trying to achieve.&amp;nbsp; We have a&amp;nbsp;content management system&amp;nbsp;where a page is basically a collection of controls.&amp;nbsp; All of the content for a&amp;nbsp;page is stored in a database.&amp;nbsp; Each control is responsible for consuming the data and rendering XHTML.&amp;nbsp; So a page would be the coming together of multiple &amp;quot;partial views&amp;quot;.&amp;nbsp; Controls are arranged on a page via zones.&lt;/p&gt;
&lt;p&gt;Currently, our CMS is web forms driven and I&amp;#39;m trying to wrap my head around how I would go about using MVC approach.&amp;nbsp; I&amp;#39;m new to ASP.Net, but have been using MVC for years with ColdFusion.&amp;nbsp; I do not care for post back and view state, so I&amp;#39;m excited about having ASP MVC.&amp;nbsp; I plan on following the thread you mentioned.&lt;/p&gt;
&lt;p&gt;I think I can collect all of the data via the model that a page needs which would be a collection of data organized by control.&amp;nbsp; I&amp;#39;m not sure how I&amp;#39;m going to dynamically call all of the controls for a page and pass to each control the data it needs.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m glad to find others working on similar approaches.&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2086633.aspx</link><pubDate>Wed, 02 Jan 2008 20:43:37 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2086633</guid><dc:creator>Kepler</dc:creator><author>Kepler</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2086633.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2086633</wfw:commentRss><description>&lt;p&gt;A partial view is just what we&amp;#39;re calling any View that doesn&amp;#39;t represent a complete&amp;nbsp;html page.&amp;nbsp; Anything that just renders a snippet of html.&lt;/p&gt;
&lt;p&gt;And, yes, Jonathan, having partial views be rendered from controller methods is absolutely necessary.&amp;nbsp; That&amp;#39;s my methodology as well.&amp;nbsp; It&amp;#39;s the only way to keep MVC intact.&amp;nbsp; Every view (partial or otherwise) should be rendered from a controller action, and not necessarily one big controller action in order to honor encapsulation.&lt;/p&gt;
&lt;p&gt;I finally got my idea working, but it&amp;#39;s a big hack calling out to other controllers and having ViewUserControls render themselves to strings.&amp;nbsp; I&amp;#39;m thinking about posting my code somewhere and starting a discussion.&amp;nbsp; I&amp;#39;m just trying to figure out the best venue.&amp;nbsp; The closest I&amp;#39;ve seen is the code posted by shinakuma in this &lt;a class="" href="http://forums.asp.net/t/1196142.aspx?PageIndex=2" target="_blank"&gt;thread&lt;/a&gt;.&amp;nbsp; That&amp;#39;s a great discussion on what we cms/portal developers are trying to accomplish.&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2086463.aspx</link><pubDate>Wed, 02 Jan 2008 18:57:45 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2086463</guid><dc:creator>Tom Schreck</dc:creator><author>Tom Schreck</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2086463.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2086463</wfw:commentRss><description>&lt;p&gt;Johnathan&lt;/p&gt;
&lt;p&gt;What&amp;#39;s a partial view?&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2071525.aspx</link><pubDate>Thu, 20 Dec 2007 23:28:33 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2071525</guid><dc:creator>Jonathan Holland</dc:creator><author>Jonathan Holland</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2071525.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2071525</wfw:commentRss><description>&lt;p&gt;I understand your point, and I think it is valid, though perhaps this is a situation where web forms is a better architecture instead of MVC. We have to be careful not to make everything into a nail now that we have the magic MVC hammer.&lt;/p&gt;&lt;p&gt;Another thought I had is that instead of having the &amp;quot;MasterView&amp;quot; pass its own Viewdata to &amp;quot;dumb&amp;quot; partial views, instead it would be able to call other controllers, which would reach into the model to get their specific data, and pass this to a partial view.&lt;/p&gt;&lt;p&gt;At this point, the master view would assemble the final page from all of the partial views.&lt;/p&gt;&lt;p&gt;But this is just thinking aloud :)&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2071509.aspx</link><pubDate>Thu, 20 Dec 2007 23:17:24 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2071509</guid><dc:creator>Kepler</dc:creator><author>Kepler</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2071509.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2071509</wfw:commentRss><description>&lt;p&gt;In most cases, I&amp;#39;d say &amp;quot;yes&amp;quot;, but not in the case of a Content Management System.&amp;nbsp; I like your MasterView idea, but you&amp;#39;d still need a controller to handle the model manipulation for that.&amp;nbsp; Something&amp;#39;s got to hit the database to determine what partial views to display on a particular page, and I think that might just be the responsibility of a specific, focused controller somewhere.&amp;nbsp; A CMS is an exception when we&amp;#39;re talking about separation of concerns, because of exactly what it&amp;#39;s trying to do.&amp;nbsp; If the business logic of your domain IS to map modules/views together onto pages, and you have database tables to facilitate that, then View manipulation is exactly what your controller will do.&amp;nbsp; Controllers handle whatever their domain requires them to handle.&amp;nbsp; It just so happens that a CMS will probably have at least one controller who&amp;#39;s primary responsibility is handling the&amp;nbsp;mapping of Views onto pages.&amp;nbsp;&amp;nbsp;That controller&amp;nbsp;might handle Views like another controller would handle Products&amp;nbsp;or Employees.&amp;nbsp; I totally agree that all the partial views should be completely ignorant, however, of anything but their own piece of the pie.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s why my idea to encapsulate all of this into RenderView is the best I&amp;#39;ve come up with so far.&amp;nbsp; Every partial view would just call his own Render for his View, then RenderView is responsible for figuring out if the parent View has been &amp;quot;built&amp;quot; and calling out to the other partial view&amp;#39;s controllers to have them render their pieces, then aggregating the results.&amp;nbsp; That way the page-building logic is in RenderView (might consider it the &amp;quot;framework&amp;quot;) and out of any specific controller.&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2071456.aspx</link><pubDate>Thu, 20 Dec 2007 22:34:42 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2071456</guid><dc:creator>Jonathan Holland</dc:creator><author>Jonathan Holland</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2071456.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2071456</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;If you are assembling multiple partial views together in a controller isn&amp;#39;t that violating the seperation of concerns between presentation and logic?&lt;/p&gt;&lt;p&gt;&amp;nbsp;Personally, I think there should be something like &amp;quot;MasterViews&amp;quot; that contain references to partial views. That way the master view logic can decide how to populate the partial views based upon the data passed to it from the controller.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2071417.aspx</link><pubDate>Thu, 20 Dec 2007 22:13:44 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2071417</guid><dc:creator>Haacked</dc:creator><author>Haacked</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2071417.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2071417</wfw:commentRss><description>&lt;p&gt;You could shorten that a bit:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;[ControllerAction]
public void About()
{
    StringWriter builder = new StringWriter();
    TextWriter originalWriter = Response.Output;
    Response.SwitchWriter(builder);
    RenderView(&amp;quot;About&amp;quot;);

    string html = builder.ToString();
    
    originalWriter.Write(html);
}
&lt;/pre&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2067066.aspx</link><pubDate>Wed, 19 Dec 2007 00:00:18 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2067066</guid><dc:creator>abombss</dc:creator><author>abombss</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2067066.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2067066</wfw:commentRss><description>&lt;p&gt;While we are at it can we fix the issue that SwitchWriter is an internal method on HttpResponse but included on the public IHttpResponse interface?&amp;nbsp; How can I ever build my own wrapper?&amp;nbsp; The interfaces can be used for more things than just testing.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;The easiest thing would be if MS opened up their wrappers and made them public with virtual methods.&amp;nbsp; Why are they internal?&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2067055.aspx</link><pubDate>Tue, 18 Dec 2007 23:44:36 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2067055</guid><dc:creator>Kepler</dc:creator><author>Kepler</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2067055.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2067055</wfw:commentRss><description>&lt;p&gt;I need that functionality as well.&amp;nbsp; In my case, I&amp;#39;m writing a content management system, and I need to dynamically render multiple partial views to a single page.&amp;nbsp; A way to capture the render of a ViewUserControl to a string would help immensely.&amp;nbsp; At a very high level, I need to do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;Get a list of partial views to be displayed on the page from the database (easy, nothing new in MVC)&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Call a controller action to render each partial view - the controller actions can be defined in the database as well or defaulted for each view&amp;nbsp;(not sure how to do this)&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Place them in the appropriate locations on the parent View&amp;nbsp;(easy if I can manage/manipulate the renderings of each partial view)&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I think I&amp;#39;ve come up with a way to handle all this in an overriden RenderView() method, but I&amp;#39;d like to hear your&amp;nbsp;insights on the above before I start a new topic on those ideas.&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2058714.aspx</link><pubDate>Thu, 13 Dec 2007 20:42:36 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2058714</guid><dc:creator>robconery</dc:creator><author>robconery</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2058714.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2058714</wfw:commentRss><description>&lt;p&gt;In the next release of the Toolkit I&amp;#39;m hoping to refactor RenderUserControl() down to the Toolkit core (rather than only expose it as an extension). This will allow you to do this with a control as well.&lt;/p&gt;
&lt;p&gt;Would it be helpful to have a ControllerExtension do this as well?&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2058562.aspx</link><pubDate>Thu, 13 Dec 2007 19:35:48 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2058562</guid><dc:creator>Tom Schreck</dc:creator><author>Tom Schreck</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2058562.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2058562</wfw:commentRss><description>&lt;p&gt;Steve&lt;/p&gt;
&lt;p&gt;Your solution allows me to accomplish my goal of capturing the View&amp;#39;s rendered output.&lt;/p&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2058534.aspx</link><pubDate>Thu, 13 Dec 2007 19:20:56 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2058534</guid><dc:creator>Tom Schreck</dc:creator><author>Tom Schreck</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2058534.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2058534</wfw:commentRss><description>&lt;p&gt;Thanks for the feedback, I&amp;#39;ll have to give this a try.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;What I&amp;#39;m envisioning is something like this:&lt;/p&gt;&lt;pre class="coloredcode"&gt;[ControllerAction]
&lt;span class="kwd"&gt;public void&lt;/span&gt; Category(&lt;span class="kwd"&gt;int&lt;/span&gt; id)
{
     Category category = GetCategoryById(id);
     &lt;span class="kwd"&gt;string&lt;/span&gt; ViewResults = RenderView(&lt;span class="st"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category);

     ...
}&lt;/pre&gt;&lt;pre class="coloredcode"&gt;Being able to capture the rendered output of a View is important because within a controller, I could call multiple views and capture those results as strings then be able to cache them or whatever else I need to do.&lt;/pre&gt;&lt;pre class="coloredcode"&gt;I use an Ajax library called Taconite and having this capability would be of incredible value with working with Taconite.&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;&lt;/pre&gt;</description></item><item><title>Re: RenderView ToString()</title><link>http://forums.asp.net/thread/2058318.aspx</link><pubDate>Thu, 13 Dec 2007 17:30:18 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2058318</guid><dc:creator>SteveSanderson</dc:creator><author>SteveSanderson</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2058318.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2058318</wfw:commentRss><description>&lt;p&gt;Yes, there are a few ways of doing that. If it&amp;#39;s in a unit test, you probably want to set up a mock IHttpResponse and read whatever gets written to it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;If, for some reason, you&amp;#39;re trying to do it from a controller, you could use a hack like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre&gt;[ControllerAction]&lt;br /&gt;public void About()&lt;br /&gt;{&lt;br /&gt;	StringWriter interceptingWriter = new StringWriter();&lt;br /&gt;	TextWriter originalWriter = this.ControllerContext.HttpContext.Response.SwitchWriter(interceptingWriter);&lt;br /&gt;	RenderView(&amp;quot;About&amp;quot;);&lt;br /&gt;&lt;br /&gt;	originalWriter.Write(&amp;quot;The text written was: &amp;quot; + interceptingWriter.ToString());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;</description></item><item><title>RenderView ToString()</title><link>http://forums.asp.net/thread/2057828.aspx</link><pubDate>Thu, 13 Dec 2007 14:07:06 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2057828</guid><dc:creator>Tom Schreck</dc:creator><author>Tom Schreck</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2057828.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=2057828</wfw:commentRss><description>&lt;p&gt;Is there a way to capture the results of RenderView() to a string?&lt;/p&gt;</description></item></channel></rss>