From 2b3687282ee0c05977beb537683500c3a7bb01a2 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Wed, 2 Apr 2014 16:15:14 -0700 Subject: [PATCH] Addressed code review comments. --- .../Shared/DisplayTemplates/Boolean.cshtml | 6 ++-- .../Shared/DisplayTemplates/Decimal.cshtml | 6 ++-- .../MvcSample.Web/Views/Shared/MyView.cshtml | 10 +++--- .../Html/HtmlHelper.cs | 36 +++++++++++++------ .../TemplatedHelpers/DisplayExtensions.cs | 23 ++++++++---- .../IHtmlHelperOfT.cs | 33 ++++++++++------- 6 files changed, 76 insertions(+), 38 deletions(-) diff --git a/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Boolean.cshtml b/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Boolean.cshtml index 2d92a8feb2..e30d2aa704 100644 --- a/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Boolean.cshtml +++ b/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Boolean.cshtml @@ -1,10 +1,12 @@ -@functions { +@using System.Globalization + +@functions { private bool? Value { get { if (ViewData.Model == null) { return null; } - return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture); + return Convert.ToBoolean(ViewData.Model, CultureInfo.InvariantCulture); } } } diff --git a/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Decimal.cshtml b/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Decimal.cshtml index adc4456610..9647bd7baa 100644 --- a/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Decimal.cshtml +++ b/samples/MvcSample.Web/Views/Shared/DisplayTemplates/Decimal.cshtml @@ -1,8 +1,10 @@ -@functions { +@using System.Globalization + +@functions { private object FormattedValue { get { if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) { - return String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model); + return String.Format(CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model); } return ViewData.TemplateInfo.FormattedModelValue; } diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml index 30c5cecede..054b6a286c 100644 --- a/samples/MvcSample.Web/Views/Shared/MyView.cshtml +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -204,6 +204,12 @@ +
+ @await Component.InvokeAsync("Tags", 15) +
+ + +
@@ -256,8 +262,4 @@
- -
- @await Component.InvokeAsync("Tags", 15) -
\ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 7820b4476b..a87a9e92b4 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -162,22 +162,17 @@ namespace Microsoft.AspNet.Mvc.Rendering return TagBuilder.CreateSanitizedId(name, IdAttributeDotReplacement); } - public virtual HtmlString Display(string expression, + public 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 metadata = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider); - var templateResult = templateBuilder.Build(); - - return new HtmlString(templateResult); + return GenerateDisplay(metadata, + htmlFieldName ?? ExpressionHelper.GetExpressionText(expression), + templateName, + additionalViewData); } /// @@ -203,6 +198,25 @@ namespace Microsoft.AspNet.Mvc.Rendering return RenderPartialCoreAsync(partialViewName, model, viewData, ViewContext.Writer); } + protected virtual HtmlString GenerateDisplay(ModelMetadata metadata, + string htmlFieldName, + string templateName, + object additionalViewData) + { + var templateBuilder = new TemplateBuilder(_viewEngine, + ViewContext, + ViewData, + metadata, + templateName, + templateName, + readOnly: true, + additionalViewData: additionalViewData); + + var templateResult = templateBuilder.Build(); + + return new HtmlString(templateResult); + } + protected virtual async Task RenderPartialCoreAsync([NotNull] string partialViewName, object model, ViewDataDictionary viewData, diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs index 3bab415600..00d8e38507 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/TemplatedHelpers/DisplayExtensions.cs @@ -1,29 +1,40 @@  namespace Microsoft.AspNet.Mvc.Rendering { - public static class DisplayExtensions + public static class HtmlHelperDisplayExtensions { - public static HtmlString Display(this IHtmlHelper html, string expression) + public static HtmlString Display([NotNull] 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) + public static HtmlString Display([NotNull] 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) + public static HtmlString Display([NotNull] 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) + public static HtmlString Display([NotNull] 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) + public static HtmlString Display([NotNull] 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 79e644bb34..cb28633a92 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -11,19 +11,6 @@ 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. /// @@ -44,6 +31,26 @@ namespace Microsoft.AspNet.Mvc.Rendering /// ViewDataDictionary ViewData { get; } + /// + /// 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 or dictionary 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); + /// /// Converts the value of the specified object to an HTML-encoded string. ///