forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelExtensions.cs
More file actions
76 lines (65 loc) · 2.32 KB
/
Copy pathLabelExtensions.cs
File metadata and controls
76 lines (65 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#if !NETSTANDARD2_0
/* ****************************************************************************
*
* 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.
*
* ***************************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;
using ServiceStack.Markdown;
namespace ServiceStack.Html
{
public static class LabelExtensions
{
public static MvcHtmlString Label(this HtmlHelper html, string expression)
{
return Label(html, expression, null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText)
{
return LabelHelper(html,
ModelMetadata.FromStringExpression(expression, html.ViewData),
expression,
labelText);
}
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return LabelFor(html, expression, null);
}
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText)
{
return LabelHelper(html,
ModelMetadata.FromLambdaExpression(expression, html.ViewData),
ExpressionHelper.GetExpressionText(expression),
labelText);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html)
{
return LabelForModel(html, null);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText);
}
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null)
{
var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(resolvedLabelText))
{
return MvcHtmlString.Empty;
}
var tag = new TagBuilder("label");
tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(htmlFieldName));
tag.SetInnerText(resolvedLabelText);
return tag.ToHtmlString(TagRenderMode.Normal);
}
}
}
#endif