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 Microsoft.AspNet.Mvc.ModelBinding
@model User
@{
ViewBag.Title = (Model == null) ? "Create Page" : "Edit Page";
@ -30,12 +31,17 @@
<ul>
@foreach (var property in metadata.Properties)
{
var propertyName = property.PropertyName;
var propertyTypeName = property.ModelType.Name;
var propertyDescription = property.Description;
<li>Property @propertyName has type @propertyTypeName and description '@propertyDescription'</li>
@PropertyListItem(property)
}
</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",
writeToMethodName: "WriteTo",
writeLiteralToMethodName: "WriteLiteralTo",
templateTypeName: "Template",
templateTypeName: "HelperResult",
defineSectionMethodName: "DefineSection")
{
ResolveUrlMethodName = "Href"

View File

@ -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()));
}
}
}
}

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);
}
}
}