50 lines
2.3 KiB
C#
50 lines
2.3 KiB
C#
/* ****************************************************************************
|
|
*
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
*
|
|
* This software is subject to the Microsoft Public License (Ms-PL).
|
|
* A copy of the license can be found in the license.htm file included
|
|
* in this distribution.
|
|
*
|
|
* You must not remove this notice, or any other, from this software.
|
|
*
|
|
* ***************************************************************************/
|
|
|
|
namespace System.Web.Mvc.Html {
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
|
|
public static class LabelExtensions {
|
|
public static MvcHtmlString Label(this HtmlHelper html, string expression) {
|
|
return LabelHelper(html,
|
|
ModelMetadata.FromStringExpression(expression, html.ViewData),
|
|
expression);
|
|
}
|
|
|
|
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
|
|
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) {
|
|
return LabelHelper(html,
|
|
ModelMetadata.FromLambdaExpression(expression, html.ViewData),
|
|
ExpressionHelper.GetExpressionText(expression));
|
|
}
|
|
|
|
public static MvcHtmlString LabelForModel(this HtmlHelper html) {
|
|
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty);
|
|
}
|
|
|
|
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName) {
|
|
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
|
|
if (String.IsNullOrEmpty(labelText)) {
|
|
return MvcHtmlString.Empty;
|
|
}
|
|
|
|
TagBuilder tag = new TagBuilder("label");
|
|
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
|
|
tag.SetInnerText(labelText);
|
|
return tag.ToMvcHtmlString(TagRenderMode.Normal);
|
|
}
|
|
}
|
|
}
|