Implement Display HTMLHelper.

Essentially add extension methods and call through to infrastructure.
This commit is contained in:
N. Taylor Mullen 2014-04-02 12:52:59 -07:00
parent 5da827b58f
commit 8477f47632
3 changed files with 63 additions and 0 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Text;
@ -161,6 +162,24 @@ namespace Microsoft.AspNet.Mvc.Rendering
return TagBuilder.CreateSanitizedId(name, IdAttributeDotReplacement);
}
public virtual HtmlString Display(string expression,
string templateName,
string htmlFieldName,
object additionalViewData)
{
var templateBuilder = new TemplateBuilder(ViewContext,
ViewData,
ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider),
htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
templateName,
readOnly: true,
additionalViewData: additionalViewData);
var templateResult = templateBuilder.Build();
return new HtmlString(templateResult);
}
/// <inheritdoc />
public virtual HtmlString Name(string name)
{

View File

@ -0,0 +1,31 @@

namespace Microsoft.AspNet.Mvc.Rendering
{
public static class DisplayExtensions
{
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression)
{
return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: null);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, object additionalViewData)
{
return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName)
{
return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: null);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName, object additionalViewData)
{
return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName, string htmlFieldName)
{
return html.Display(expression, templateName, htmlFieldName, additionalViewData: null);
}
}
}

View File

@ -11,6 +11,19 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <typeparam name="TModel">The <see cref="Type"/> of the model.</typeparam>
public interface IHtmlHelper<TModel>
{
/// <summary>
/// Returns HTML markup for each property in the object that is represented by the expression, using the specified template, HTML field ID, and additional view data.
/// </summary>
/// <param name="expression">An expression that identifies the object that contains the properties to display.</param>
/// <param name="templateName">The name of the template that is used to render the object.</param>
/// <param name="htmlFieldName">A string that is used to disambiguate the names of HTML input elements that are rendered for properties that have the same name.</param>
/// <param name="additionalViewData">An anonymous object that can contain additional view data that will be merged into the <see cref="ViewDataDictionary{TModel}"/> instance that is created for the template.</param>
/// <returns>The HTML markup for each property in the object that is represented by the expression.</returns>
HtmlString Display(string expression,
string templateName,
string htmlFieldName,
object additionalViewData);
/// <summary>
/// Gets or sets the character that replaces periods in the ID attribute of an element.
/// </summary>