From 52ce83db9e2a23348b99519b5ca59e2d30fff60a Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Wed, 2 Apr 2014 15:25:45 -0700 Subject: [PATCH] Implement DisplayFor Html Helper. Essentially add extension methods and call through to infrastructure. --- .../Html/HtmlHelper.cs | 18 +++++++++ .../TemplatedHelpers/DisplayForExtensions.cs | 39 +++++++++++++++++++ .../IHtmlHelperOfT.cs | 15 +++++++ 3 files changed, 72 insertions(+) create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index a87a9e92b4..f4a266fa98 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -175,6 +175,24 @@ namespace Microsoft.AspNet.Mvc.Rendering additionalViewData); } + public virtual HtmlString DisplayFor(Expression> expression, + string templateName, + string htmlFieldName, + object additionalViewData) + { + var templateBuilder = new TemplateBuilder(ViewContext, + ViewData, + ExpressionMetadataProvider.FromLambdaExpression(expression, (ViewDataDictionary)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/DisplayForExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForExtensions.cs new file mode 100644 index 0000000000..f300357d25 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForExtensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public static class DisplayForExtensions + { + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] + public static HtmlString DisplayFor(this IHtmlHelper html, Expression> expression) + { + return html.DisplayFor(expression, templateName: null, htmlFieldName: null, additionalViewData: null); + } + + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] + public static HtmlString DisplayFor(this IHtmlHelper html, Expression> expression, object additionalViewData) + { + return html.DisplayFor(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData); + } + + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] + public static HtmlString DisplayFor(this IHtmlHelper html, Expression> expression, string templateName) + { + return html.DisplayFor(expression, templateName, htmlFieldName: null, additionalViewData: null); + } + + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] + public static HtmlString DisplayFor(this IHtmlHelper html, Expression> expression, string templateName, object additionalViewData) + { + return html.DisplayFor(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData); + } + + [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] + public static HtmlString DisplayFor(this IHtmlHelper html, Expression> expression, string templateName, string htmlFieldName) + { + return html.DisplayFor(expression, templateName: templateName, htmlFieldName: htmlFieldName, additionalViewData: null); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs index cb28633a92..7431c353e8 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -51,6 +51,21 @@ namespace Microsoft.AspNet.Mvc.Rendering string htmlFieldName, object additionalViewData); + /// + /// Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data. + /// + /// The type of the model. + /// The type of the value. + /// 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 DisplayFor(Expression> expression, + string templateName, + string htmlFieldName, + object additionalViewData); + /// /// Converts the value of the specified object to an HTML-encoded string. ///