'@Html.DisplayFor(model => model.GPA)'
diff --git a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj
index fa6ec36996..fa0472d5e8 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj
+++ b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj
@@ -141,7 +141,9 @@
+
+
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
index 72f3938a12..b394853184 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
@@ -224,6 +224,18 @@ namespace Microsoft.AspNet.Mvc.Rendering
templateName,
additionalViewData);
}
+
+ public HtmlString DisplayName(string expression)
+ {
+ var modelMetadata = string.IsNullOrEmpty(expression) ?
+ ViewData.ModelMetadata :
+ ExpressionMetadataProvider.FromStringExpression(
+ expression,
+ ViewData,
+ MetadataProvider);
+ return GenerateDisplayName(modelMetadata, expression);
+ }
+
public HtmlString DropDownList(string name, IEnumerable selectList, string optionLabel,
object htmlAttributes)
@@ -241,6 +253,22 @@ namespace Microsoft.AspNet.Mvc.Rendering
return GenerateHidden(metadata: null, name: name, value: value, useViewData: (value == null),
htmlAttributes: htmlAttributes);
}
+
+
+ public HtmlString Label(string expression, string labelText, object htmlAttributes)
+ {
+ var modelMetadata = string.IsNullOrEmpty(expression)?
+ ViewData.ModelMetadata :
+ ExpressionMetadataProvider.FromStringExpression(
+ expression,
+ ViewData,
+ MetadataProvider);
+ return GenerateLabel(
+ modelMetadata,
+ expression,
+ labelText,
+ htmlAttributes);
+ }
public virtual HtmlString Name(string name)
{
@@ -543,6 +571,23 @@ namespace Microsoft.AspNet.Mvc.Rendering
format: null,
htmlAttributes: htmlAttributeDictionary);
}
+
+ protected virtual HtmlString GenerateDisplayName([NotNull] ModelMetadata metadata, string htmlFieldName)
+ {
+ // We don't call ModelMetadata.GetDisplayName here because
+ // we want to fall back to the field name rather than the ModelType.
+ // This is similar to how the GenerateLabel get the text of a label.
+ // TODO: This needs to be updated after ModelMetadata has a DisplayName property
+ var resolvedDisplayName = metadata.PropertyName;
+ if (resolvedDisplayName == null)
+ {
+ resolvedDisplayName = string.IsNullOrEmpty(htmlFieldName) ?
+ string.Empty :
+ htmlFieldName.Split('.').Last();
+ }
+
+ return new HtmlString(Encode(resolvedDisplayName));
+ }
protected HtmlString GenerateDropDown(ModelMetadata metadata, string expression,
IEnumerable selectList, string optionLabel, object htmlAttributes)
@@ -632,6 +677,36 @@ namespace Microsoft.AspNet.Mvc.Rendering
format: null,
htmlAttributes: htmlAttributeDictionary);
}
+
+ protected virtual HtmlString GenerateLabel([NotNull] ModelMetadata metadata,
+ string htmlFieldName,
+ string labelText,
+ object htmlAttributes)
+ {
+ // TODO: This needs to be updated after ModelMetadata has a DisplayName property
+ string resolvedLabelText = labelText ?? metadata.PropertyName;
+ if (resolvedLabelText == null)
+ {
+ resolvedLabelText = string.IsNullOrEmpty(htmlFieldName) ?
+ string.Empty :
+ htmlFieldName.Split('.').Last();
+ }
+
+ if (string.IsNullOrEmpty(resolvedLabelText))
+ {
+ return HtmlString.Empty;
+ }
+
+ TagBuilder tag = new TagBuilder("label");
+ tag.Attributes.Add(
+ "for",
+ TagBuilder.CreateSanitizedId(
+ ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName),
+ IdAttributeDotReplacement));
+ tag.SetInnerText(resolvedLabelText);
+ tag.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes), replaceExisting: true);
+ return tag.ToHtmlString(TagRenderMode.Normal);
+ }
protected virtual HtmlString GenerateLink(
[NotNull] string linkText,
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs
index c2af9e14c2..3308d35b0b 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs
@@ -82,6 +82,31 @@ namespace Microsoft.AspNet.Mvc.Rendering
additionalViewData);
}
+ ///
+ public HtmlString DisplayNameFor([NotNull] Expression> expression)
+ {
+ var metadata = GetModelMetadata(expression);
+ return GenerateDisplayName(metadata, ExpressionHelper.GetExpressionText(expression));
+ }
+
+ ///
+ public HtmlString DisplayNameForInnerType(Expression> expression)
+ {
+ var metadata = ExpressionMetadataProvider.
+ FromLambdaExpression(
+ expression,
+ new ViewDataDictionary(
+ MetadataProvider),
+ MetadataProvider);
+ var expressionText = ExpressionHelper.GetExpressionText(expression);
+ if (metadata == null)
+ {
+ throw new InvalidOperationException(Resources.FormatHtmlHelper_NullModelMetadata(expressionText));
+ }
+
+ return GenerateDisplayName(metadata, expressionText);
+ }
+
///
public HtmlString HiddenFor([NotNull] Expression> expression,
object htmlAttributes)
@@ -91,6 +116,13 @@ namespace Microsoft.AspNet.Mvc.Rendering
htmlAttributes: htmlAttributes);
}
+ ///
+ public HtmlString LabelFor([NotNull] Expression> expression, string labelText, object htmlAttributes)
+ {
+ var metadata = GetModelMetadata(expression);
+ return GenerateLabel(metadata, ExpressionHelper.GetExpressionText(expression), labelText, htmlAttributes);
+ }
+
///
public HtmlString NameFor([NotNull] Expression> expression)
{
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperDisplayNameExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperDisplayNameExtensions.cs
new file mode 100644
index 0000000000..e4ed3cf7d7
--- /dev/null
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperDisplayNameExtensions.cs
@@ -0,0 +1,36 @@
+
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+namespace Microsoft.AspNet.Mvc.Rendering
+{
+ ///
+ /// DisplayName-related extensions for and .
+ ///
+ public static class HtmlHelperDisplayNameExtensions
+ {
+ ///
+ /// Gets the display name for the current model.
+ ///
+ /// The instance that this method extends.
+ /// An that represents HTML markup.
+ public static HtmlString DisplayNameForModel([NotNull] this IHtmlHelper htmlHelper)
+ {
+ return htmlHelper.DisplayName(string.Empty);
+ }
+
+ ///
+ /// Gets the display name for the model.
+ ///
+ /// The instance that this method extends.
+ /// An expression that identifies the object that contains the display name.
+ ///
+ /// The display name for the model.
+ ///
+ public static HtmlString DisplayNameFor(this IHtmlHelper> htmlHelper,
+ Expression> expression)
+ {
+ return htmlHelper.DisplayNameForInnerType(expression);
+ }
+ }
+}
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLabelExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLabelExtensions.cs
new file mode 100644
index 0000000000..ed16dab542
--- /dev/null
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLabelExtensions.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Linq.Expressions;
+
+namespace Microsoft.AspNet.Mvc.Rendering
+{
+ public static class HtmlHelperLabelExtensions
+ {
+ public static HtmlString Label([NotNull] this IHtmlHelper html, string expression)
+ {
+ return html.Label(expression,
+ labelText: null,
+ htmlAttributes: null);
+ }
+
+ public static HtmlString Label([NotNull] this IHtmlHelper html,
+ string expression,
+ string labelText)
+ {
+ return html.Label(expression, labelText, htmlAttributes: null);
+ }
+
+ public static HtmlString LabelFor([NotNull] this IHtmlHelper html,
+ [NotNull] Expression> expression)
+ {
+ return html.LabelFor(expression, labelText: null, htmlAttributes: null);
+ }
+
+ public static HtmlString LabelFor([NotNull] this IHtmlHelper html,
+ [NotNull] Expression> expression,
+ string labelText)
+ {
+ return html.LabelFor(expression, labelText, htmlAttributes: null);
+ }
+
+ public static HtmlString LabelFor([NotNull] this IHtmlHelper html,
+ [NotNull] Expression> expression,
+ object htmlAttributes)
+ {
+ return html.LabelFor(expression, labelText: null, htmlAttributes: htmlAttributes);
+ }
+
+ public static HtmlString LabelForModel([NotNull] this IHtmlHelper html)
+ {
+ return LabelForModel(html, labelText: null);
+ }
+
+ public static HtmlString LabelForModel([NotNull] this IHtmlHelper html, string labelText)
+ {
+ return html.Label(expression: string.Empty, labelText: labelText, htmlAttributes: null);
+ }
+
+ public static HtmlString LabelForModel([NotNull] this IHtmlHelper html,
+ object htmlAttributes)
+ {
+ return html.Label(expression: string.Empty, labelText: null, htmlAttributes: htmlAttributes);
+ }
+
+ public static HtmlString LabelForModel([NotNull] this IHtmlHelper html,
+ string labelText,
+ object htmlAttributes)
+ {
+ return html.Label(expression: string.Empty, labelText: labelText, htmlAttributes: htmlAttributes);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
index 07d11c07af..976989bdcc 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs
@@ -174,6 +174,34 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// The HTML markup for each property in the model.
HtmlString DisplayForModel(string templateName, string htmlFieldName, object additionalViewData);
+ ///
+ /// Gets the display name.
+ ///
+ /// An expression that identifies the object that contains the display name.
+ ///
+ /// The display name.
+ ///
+ HtmlString DisplayName(string expression);
+
+ ///
+ /// Gets the display name for the model.
+ ///
+ /// An expression that identifies the object that contains the display name.
+ /// The type of the value.
+ ///
+ /// The display name for the model.
+ ///
+ HtmlString DisplayNameFor(Expression> expression);
+
+ ///
+ /// Gets the display name for the inner model if the current model represents a collection.
+ ///
+ /// The type of the inner model
+ /// The type of the value.
+ /// An expression that identifies the object that contains the display name.
+ /// The display name for the inner model.
+ HtmlString DisplayNameForInnerType(Expression> expression);
+
///
/// Returns a single-selection HTML {select} element using the specified name of the form field,
/// list items, option label, and HTML attributes.
@@ -269,6 +297,30 @@ namespace Microsoft.AspNet.Mvc.Rendering
HtmlString HiddenFor([NotNull] Expression> expression,
object htmlAttributes);
+ ///
+ /// Returns an HTML label element and the property name of the property that is represented by the specified expression.
+ ///
+ /// An expression that identifies the property to display.
+ /// The label text.
+ /// An object that contains the HTML attributes to set for the element.
+ ///
+ /// An HTML label element and the property name of the property that is represented by the expression.
+ ///
+ HtmlString Label(string expression, string labelText, object htmlAttributes);
+
+ ///
+ /// Returns an HTML label element and the property name of the property that is represented by the specified expression.
+ ///
+ /// An expression that identifies the property to display.
+ /// An object that contains the HTML attributes to set for the element.
+ /// The type of the value.
+ ///
+ /// An HTML label element and the property name of the property that is represented by the expression.
+ ///
+ HtmlString LabelFor(Expression> expression,
+ string labelText,
+ object htmlAttributes);
+
///
/// Gets the full HTML field name for the given expression .
///