From 5f5273689800e0903e07cddebf2c8ab36854a0fc Mon Sep 17 00:00:00 2001 From: Rafal Spacjer Date: Mon, 3 Oct 2011 18:18:54 +0200 Subject: [PATCH] Partial method in HtmlHelpers updated to fix a problem with @Html.Partial statement in Razor view --- .../RazorViewCompilerFixture.cs | 30 +++++++++++++++++++ src/Nancy.ViewEngines.Razor/HtmlHelpers.cs | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Nancy.ViewEngines.Razor.Tests/RazorViewCompilerFixture.cs b/src/Nancy.ViewEngines.Razor.Tests/RazorViewCompilerFixture.cs index 40e5ce9622..e7fc78f7fd 100644 --- a/src/Nancy.ViewEngines.Razor.Tests/RazorViewCompilerFixture.cs +++ b/src/Nancy.ViewEngines.Razor.Tests/RazorViewCompilerFixture.cs @@ -49,5 +49,35 @@ public void GetCompiledView_should_render_to_stream() // Then stream.ShouldEqual("

Hello Mr. test

"); } + + [Fact] + public void Should_be_able_to_render_view_with_partial_to_stream() + { + // Given + var location = new ViewLocationResult( + string.Empty, + string.Empty, + "cshtml", + () => new StringReader(@"@{var x = ""test"";}

Hello Mr. @x

@Html.Partial(""partial.cshtml"")") + ); + + var partialLocation = new ViewLocationResult( + string.Empty, + "partial.cshtml", + "cshtml", + () => new StringReader(@"this is partial") + ); + + A.CallTo(() => this.renderContext.LocateView("partial.cshtml",null)).Returns(partialLocation); + + var stream = new MemoryStream(); + + // When + var response = this.engine.RenderView(location, null,this.renderContext); + response.Contents.Invoke(stream); + + // Then + stream.ShouldEqual("

Hello Mr. test

this is partial"); + } } } diff --git a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs index 5f56fb696f..961455c83f 100644 --- a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs @@ -29,7 +29,8 @@ public IHtmlString Partial(string viewName, dynamic model) { ViewLocationResult view = this.renderContext.LocateView(viewName, model); - Action action = this.engine.RenderView(view, model, this.renderContext); + Response response = this.engine.RenderView(view, model, this.renderContext); + Action action = response.Contents; var mem = new MemoryStream(); action.Invoke(mem);