From 49de9d2828925fcf6caddd4fcde8be135ddfac71 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 1 Apr 2014 13:24:22 -0700 Subject: [PATCH] Adding Value Html Helpers --- .../MvcSample.Web/Views/Shared/MyView.cshtml | 12 ++++++ .../Html/HtmlHelper.cs | 42 +++++++++++++++++++ .../Html/HtmlHelperOfT.cs | 6 +++ .../HtmlHelperValueExtensions.cs | 28 +++++++++++++ .../IHtmlHelperOfT.cs | 18 +++++++- 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperValueExtensions.cs diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml index 9f8a781e71..fe8b6cb9ab 100644 --- a/samples/MvcSample.Web/Views/Shared/MyView.cshtml +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -71,6 +71,9 @@ '@Html.NameForModel()' + + '@Html.ValueForModel()' + @@ -79,6 +82,9 @@ '@Html.Name("Name")' + + '@Html.Value("Name")' + @@ -87,6 +93,9 @@ '@Html.NameFor(m => m.Address)' + + '@Html.ValueFor(m => m.Address)' + @@ -95,6 +104,9 @@ '@Html.Name("Anon.Address.Street")' + + '@Html.Value("Anon.Address.Street")' + diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 47ba0034d9..b1c64b482e 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Mvc.ModelBinding; +using Microsoft.AspNet.Mvc.Rendering.Expressions; namespace Microsoft.AspNet.Mvc.Rendering { @@ -300,6 +301,11 @@ namespace Microsoft.AspNet.Mvc.Rendering htmlAttributes: htmlAttributes); } + public HtmlString Value([NotNull] string name, string format) + { + return GenerateValue(name, value: null, format: format, useViewData: true); + } + protected string EvalString(string key, string format) { return Convert.ToString(ViewData.Eval(key, format), CultureInfo.CurrentCulture); @@ -394,6 +400,42 @@ namespace Microsoft.AspNet.Mvc.Rendering return tagBuilder.ToHtmlString(TagRenderMode.SelfClosing); } + + protected virtual HtmlString GenerateValue(string name, object value, string format, bool useViewData) + { + var fullName = ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); + var attemptedValue = (string)GetModelStateValue(fullName, typeof(string)); + + string resolvedValue; + if (attemptedValue != null) + { + // case 1: if ModelState has a value then it's already formatted so ignore format string + resolvedValue = attemptedValue; + } + else if (useViewData) + { + if (name.Length == 0) + { + // case 2(a): format the value from ModelMetadata for the current model + var metadata = ViewData.ModelMetadata; + resolvedValue = FormatValue(metadata.Model, format); + } + else + { + // case 2(b): format the value from ViewData + resolvedValue = EvalString(name, format); + } + } + else + { + // case 3: format the explicit value from ModelMetadata + resolvedValue = FormatValue(value, format); + } + + return new HtmlString(Encode(resolvedValue)); + } + + private static string GetInputTypeString(InputType inputType) { switch (inputType) diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs index eda6d32ac1..aaec0c699d 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelperOfT.cs @@ -72,5 +72,11 @@ namespace Microsoft.AspNet.Mvc.Rendering return metadata; } + + public HtmlString ValueFor(Expression> expression, string format) + { + var metadata = GetModelMetadata(expression); + return GenerateValue(ExpressionHelper.GetExpressionText(expression), metadata.Model, format, useViewData: false); + } } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperValueExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperValueExtensions.cs new file mode 100644 index 0000000000..9a3719354d --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Rendering/HtmlHelperValueExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq.Expressions; + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public static class HtmlHelperValueExtensions + { + public static HtmlString Value([NotNull] this IHtmlHelper htmlHelper, string name) + { + return htmlHelper.Value(name, format: null); + } + + public static HtmlString ValueFor([NotNull] this IHtmlHelper htmlHelper, Expression> expression) + { + return htmlHelper.ValueFor(expression, format: null); + } + + public static HtmlString ValueForModel([NotNull] this IHtmlHelper htmlHelper) + { + return ValueForModel(htmlHelper, format: null); + } + + public static HtmlString ValueForModel([NotNull] this IHtmlHelper htmlHelper, string format) + { + return htmlHelper.Value(string.Empty, format); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs index 47f5903851..aa723347c3 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IHtmlHelperOfT.cs @@ -130,7 +130,7 @@ namespace Microsoft.AspNet.Mvc.Rendering /// /// New containing the rendered HTML. HtmlString TextBoxFor([NotNull] Expression> expression, string format, - IDictionary htmlAttributes); + IDictionary htmlAttributes); /// /// Returns an unordered list (ul element) of validation messages that are in the @@ -144,5 +144,21 @@ namespace Microsoft.AspNet.Mvc.Rendering /// HtmlString ValidationSummary(bool excludePropertyErrors, string message, IDictionary htmlAttributes); + + /// + /// Returns the model value for the given expression . + /// + /// Name of an expression, relative to the current model. + /// The optional format string to apply to the value. + /// An that represents HTML markup. + HtmlString Value([NotNull] string name, string format); + + /// + /// Returns the model value for the given expression . + /// + /// An expression, relative to the current model. + /// The optional format string to apply to the value. + /// An that represents HTML markup. + HtmlString ValueFor([NotNull] Expression> expression, string format); } }