Revive HelperResult to allow using @helper in views

This commit is contained in:
Pranav K 2014-03-27 11:37:32 -07:00
parent 655b329dd0
commit f1efbde29d
4 changed files with 50 additions and 10 deletions

View File

@ -1,4 +1,5 @@
@using MvcSample.Web.Models @using MvcSample.Web.Models
@using Microsoft.AspNet.Mvc.ModelBinding
@model User @model User
@{ @{
ViewBag.Title = (Model == null) ? "Create Page" : "Edit Page"; ViewBag.Title = (Model == null) ? "Create Page" : "Edit Page";
@ -30,12 +31,17 @@
<ul> <ul>
@foreach (var property in metadata.Properties) @foreach (var property in metadata.Properties)
{ {
var propertyName = property.PropertyName; @PropertyListItem(property)
var propertyTypeName = property.ModelType.Name;
var propertyDescription = property.Description;
<li>Property @propertyName has type @propertyTypeName and description '@propertyDescription'</li>
} }
</ul> </ul>
} }
} }
</div> </div>
@helper PropertyListItem(ModelMetadata property)
{
var propertyName = property.PropertyName;
var propertyTypeName = property.ModelType.Name;
<li>Property @propertyName is type @propertyTypeName and '@(property.Description ?? "no description")'</li>
}

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Razor
writeLiteralMethodName: "WriteLiteral", writeLiteralMethodName: "WriteLiteral",
writeToMethodName: "WriteTo", writeToMethodName: "WriteTo",
writeLiteralToMethodName: "WriteLiteralTo", writeLiteralToMethodName: "WriteLiteralTo",
templateTypeName: "Template", templateTypeName: "HelperResult",
defineSectionMethodName: "DefineSection") defineSectionMethodName: "DefineSection")
{ {
ResolveUrlMethodName = "Href" ResolveUrlMethodName = "Href"

View File

@ -97,10 +97,24 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
if (content != null) if (content != null)
{ {
var htmlString = content as HtmlString; var helperResult = content as HelperResult;
var contentToWrite = htmlString != null ? content.ToString() : if (helperResult != null)
WebUtility.HtmlEncode(content.ToString()); {
writer.Write(contentToWrite); helperResult.WriteTo(writer);
}
else
{
var htmlString = content as HtmlString;
if (htmlString != null)
{
writer.Write(content.ToString());
}
else
{
writer.Write(WebUtility.HtmlEncode(content.ToString()));
}
}
} }
} }

View File

@ -0,0 +1,20 @@
using System;
using System.IO;
namespace Microsoft.AspNet.Mvc.Rendering
{
public class HelperResult
{
private readonly Action<TextWriter> _action;
public HelperResult([NotNull] Action<TextWriter> action)
{
_action = action;
}
public void WriteTo([NotNull] TextWriter writer)
{
_action(writer);
}
}
}