Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / Html / LabelExtensions.cs
1 namespace System.Web.Mvc.Html {
2     using System;
3     using System.Diagnostics.CodeAnalysis;
4     using System.Linq;
5     using System.Linq.Expressions;
6
7     public static class LabelExtensions {
8         public static MvcHtmlString Label(this HtmlHelper html, string expression) {
9             return Label(html,
10                          expression,
11                          null);
12         }
13
14         public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText) {
15             return LabelHelper(html,
16                                ModelMetadata.FromStringExpression(expression, html.ViewData),
17                                expression,
18                                labelText);
19         }
20
21         [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
22         public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) {
23             return LabelFor<TModel, TValue>(html, expression, null);
24         }
25
26         [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
27         public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText) {
28             return LabelHelper(html,
29                                ModelMetadata.FromLambdaExpression(expression, html.ViewData),
30                                ExpressionHelper.GetExpressionText(expression),
31                                labelText);
32         }
33
34         public static MvcHtmlString LabelForModel(this HtmlHelper html) {
35             return LabelForModel(html, null);
36         }
37
38         public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText) {
39             return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText);
40         }
41
42         internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null) {
43             string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
44             if (String.IsNullOrEmpty(resolvedLabelText)) {
45                 return MvcHtmlString.Empty;
46             }
47
48             TagBuilder tag = new TagBuilder("label");
49             tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
50             tag.SetInnerText(resolvedLabelText);
51             return tag.ToMvcHtmlString(TagRenderMode.Normal);
52         }
53     }
54 }