From 1f47abd758038bce280da89ac637b24ca445e4b9 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Wed, 2 Apr 2014 16:11:53 -0700 Subject: [PATCH] Implement DisplayForModel Html Helper. Essentially add extension methods and call through to infrastructure. --- .../Html/HtmlHelper.cs | 17 ++++++++++ .../DisplayForModelExtensions.cs | 31 +++++++++++++++++++ .../IHtmlHelperOfT.cs | 9 ++++++ 3 files changed, 57 insertions(+) create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForModelExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 6f7b403bc5..278cf11061 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -175,6 +175,23 @@ namespace Microsoft.AspNet.Mvc.Rendering additionalViewData); } + public virtual HtmlString DisplayForModel(string templateName, + string htmlFieldName, + object additionalViewData) + { + var templateBuilder = new TemplateBuilder(ViewContext, + ViewData, + ViewData.ModelMetadata, + htmlFieldName, + 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/DisplayForModelExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForModelExtensions.cs new file mode 100644 index 0000000000..776612fe4f --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayForModelExtensions.cs @@ -0,0 +1,31 @@ + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public static class DisplayForModelExtensions + { + public static HtmlString DisplayForModel(this IHtmlHelper html) + { + return html.DisplayForModel(templateName: null, htmlFieldName: null, additionalViewData: null); + } + + public static HtmlString DisplayForModel(this IHtmlHelper html, object additionalViewData) + { + return html.DisplayForModel(templateName: null, htmlFieldName: null, additionalViewData: additionalViewData); + } + + public static HtmlString DisplayForModel(this IHtmlHelper html, string templateName) + { + return html.DisplayForModel(templateName, htmlFieldName: null, additionalViewData: null); + } + + public static HtmlString DisplayForModel(this IHtmlHelper html, string templateName, object additionalViewData) + { + return html.DisplayForModel(templateName, htmlFieldName: null, additionalViewData: additionalViewData); + } + + public static HtmlString DisplayForModel(this IHtmlHelper html, string templateName, string htmlFieldName) + { + return html.DisplayForModel(templateName, htmlFieldName, additionalViewData: null); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs index 1551a06dfc..fd9936af33 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -73,6 +73,15 @@ namespace Microsoft.AspNet.Mvc.Rendering string htmlFieldName, object additionalViewData); + /// + /// Returns HTML markup for each property in the model, using the specified template, an HTML field ID, and additional view data. + /// + /// 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 model. + HtmlString DisplayForModel(string templateName, string htmlFieldName, object additionalViewData); + /// /// Converts the value of the specified object to an HTML-encoded string. ///