I just installed ASP.NET MVC 3 RC to try and upgrade an MVC 2 site. I encountered a rendering problem which I managed to repro outside of the site, using an MVC 3 project created from scratch.
Here is my Razor cshtml view:
@using Mvc3RCTest.Helpers
<h2>Demo Render Bug</h2>
<div class="content">
@{ Html.RenderTest(); }
</div>
RenderTest is an HTML extension defined as follows:
using System.Web;
using System.Web.Mvc;
namespace Mvc3RCTest.Helpers
{
public static class TestHtmlExtensions
{
public static void RenderTest(this HtmlHelper html)
{
HttpResponseBase r = html.ViewContext.HttpContext.Response;
r.Write("<ul>");
for (int i = 0; i < 10; ++i)
{
r.Write("<li>" + i + "</li>");
}
r.Write("</ul>");
}
}
}
When this is rendered, the HTML looks like the following:
<ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
<h2>Demo Render Bug</h2>
<div class="content">
</div>
As you can see, the output of the RenderTest HTML extension was incorrectly emitted before the rest of the Razor template. It seems as if the Razor rendering engine is trying to cache the entire output, without being aware that HTML extensions can write directly to the output.
Has anyone else seen this problem? Anyone know how to work around this, without having to redo all of my HTML extensions not to write directly to the output?