From f1efbde29daed5da6076727ba79a357444de1ae2 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 27 Mar 2014 11:37:32 -0700 Subject: [PATCH] Revive HelperResult to allow using @helper in views --- .../MvcSample.Web/Views/Home/Create.cshtml | 16 +++++++++----- .../MvcRazorHost.cs | 2 +- src/Microsoft.AspNet.Mvc.Razor/RazorView.cs | 22 +++++++++++++++---- .../View/HelperResult.cs | 20 +++++++++++++++++ 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/View/HelperResult.cs diff --git a/samples/MvcSample.Web/Views/Home/Create.cshtml b/samples/MvcSample.Web/Views/Home/Create.cshtml index 6f71264af2..1220da9603 100644 --- a/samples/MvcSample.Web/Views/Home/Create.cshtml +++ b/samples/MvcSample.Web/Views/Home/Create.cshtml @@ -1,4 +1,5 @@ @using MvcSample.Web.Models +@using Microsoft.AspNet.Mvc.ModelBinding @model User @{ ViewBag.Title = (Model == null) ? "Create Page" : "Edit Page"; @@ -30,12 +31,17 @@ } } - \ No newline at end of file + + +@helper PropertyListItem(ModelMetadata property) +{ + var propertyName = property.PropertyName; + var propertyTypeName = property.ModelType.Name; + +
  • Property @propertyName is type @propertyTypeName and '@(property.Description ?? "no description")'
  • +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs index b59bc07e1d..fe41055420 100644 --- a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs +++ b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Razor writeLiteralMethodName: "WriteLiteral", writeToMethodName: "WriteTo", writeLiteralToMethodName: "WriteLiteralTo", - templateTypeName: "Template", + templateTypeName: "HelperResult", defineSectionMethodName: "DefineSection") { ResolveUrlMethodName = "Href" diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 849bc80181..8629865611 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -97,10 +97,24 @@ namespace Microsoft.AspNet.Mvc.Razor { if (content != null) { - var htmlString = content as HtmlString; - var contentToWrite = htmlString != null ? content.ToString() : - WebUtility.HtmlEncode(content.ToString()); - writer.Write(contentToWrite); + var helperResult = content as HelperResult; + if (helperResult != null) + { + helperResult.WriteTo(writer); + + } + else + { + var htmlString = content as HtmlString; + if (htmlString != null) + { + writer.Write(content.ToString()); + } + else + { + writer.Write(WebUtility.HtmlEncode(content.ToString())); + } + } } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/HelperResult.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/HelperResult.cs new file mode 100644 index 0000000000..74f6b36991 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/HelperResult.cs @@ -0,0 +1,20 @@ +using System; +using System.IO; + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public class HelperResult + { + private readonly Action _action; + + public HelperResult([NotNull] Action action) + { + _action = action; + } + + public void WriteTo([NotNull] TextWriter writer) + { + _action(writer); + } + } +}