From 8477f4763201a41b515366d88778f983eeb567d3 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Wed, 2 Apr 2014 12:52:59 -0700 Subject: [PATCH] Implement Display HTMLHelper. Essentially add extension methods and call through to infrastructure. --- .../Html/HtmlHelper.cs | 19 ++++++++++++ .../TemplatedHelpers/DisplayExtensions.cs | 31 +++++++++++++++++++ .../IHtmlHelperOfT.cs | 13 ++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 68c0a5863b..7820b4476b 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -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); + } + /// public virtual HtmlString Name(string name) { diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs new file mode 100644 index 0000000000..3bab415600 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs @@ -0,0 +1,31 @@ + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public static class DisplayExtensions + { + public static HtmlString Display(this IHtmlHelper html, string expression) + { + return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: null); + } + + public static HtmlString Display(this IHtmlHelper html, string expression, object additionalViewData) + { + return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData); + } + + public static HtmlString Display(this IHtmlHelper html, string expression, string templateName) + { + return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: null); + } + + public static HtmlString Display(this IHtmlHelper html, string expression, string templateName, object additionalViewData) + { + return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData); + } + + public static HtmlString Display(this IHtmlHelper html, string expression, string templateName, string htmlFieldName) + { + return html.Display(expression, templateName, htmlFieldName, additionalViewData: null); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs index aa723347c3..79e644bb34 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -11,6 +11,19 @@ namespace Microsoft.AspNet.Mvc.Rendering /// The of the model. public interface IHtmlHelper { + /// + /// 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. + /// + /// An expression that identifies the object that contains the properties to display. + /// The name of the template that is used to render the object. + /// A string that is used to disambiguate the names of HTML input elements that are rendered for properties that have the same name. + /// An anonymous object that can contain additional view data that will be merged into the instance that is created for the template. + /// The HTML markup for each property in the object that is represented by the expression. + HtmlString Display(string expression, + string templateName, + string htmlFieldName, + object additionalViewData); + /// /// Gets or sets the character that replaces periods in the ID attribute of an element. ///