-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLabelExtensions.cs
More file actions
117 lines (101 loc) · 4.49 KB
/
LabelExtensions.cs
File metadata and controls
117 lines (101 loc) · 4.49 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq.Expressions;
namespace JavaScriptEngineSwitcher.Sample.AspNet4.Mvc4.Infrastructure.Helpers
{
public static class LabelExtensions
{
public static MvcHtmlString CustomLabel(this HtmlHelper htmlHelper, string name, string labelText,
bool isRequired, bool withColon, object htmlAttributes)
{
return htmlHelper.CustomLabel(name, labelText, isRequired, withColon,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString CustomLabel(this HtmlHelper htmlHelper, string name, string labelText,
bool isRequired, bool withColon, IDictionary<string, object> htmlAttributes)
{
string id = TagBuilder.CreateSanitizedId(name);
var label = new TagBuilder("label");
label.MergeAttribute("for", id);
if (htmlAttributes != null)
{
label.MergeAttributes(htmlAttributes);
}
label.InnerHtml = labelText + ((withColon) ? ":" : String.Empty);
if (isRequired)
{
var requiredFieldMarker = new TagBuilder("span");
requiredFieldMarker.SetInnerText("*");
requiredFieldMarker.AddCssClass(htmlHelper.Constants().RequiredFieldMarkerCssClassName);
label.InnerHtml += requiredFieldMarker.ToString();
}
return MvcHtmlString.Create(label.ToString());
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression)
{
return htmlHelper.CustomLabelFor(expression, false, false, null);
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
return htmlHelper.CustomLabelFor(expression, false, false,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.CustomLabelFor(expression, false, false, htmlAttributes);
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon)
{
return htmlHelper.CustomLabelFor(expression, withColon, false, null);
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon, object htmlAttributes)
{
return htmlHelper.CustomLabelFor(expression, withColon, false,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.CustomLabelFor(expression, withColon, false, htmlAttributes);
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon, bool hideRequiredFieldMarker)
{
return htmlHelper.CustomLabelFor(expression, withColon, hideRequiredFieldMarker, null);
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon, bool hideRequiredFieldMarker,
object htmlAttributes)
{
return htmlHelper.CustomLabelFor(expression, withColon, hideRequiredFieldMarker,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString CustomLabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression, bool withColon, bool hideRequiredFieldMarker,
IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
string name = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName;
bool isRequired = false;
if (!hideRequiredFieldMarker && !metadata.IsReadOnly && metadata.ShowForEdit)
{
foreach (ModelValidator validator in metadata.GetValidators(htmlHelper.ViewContext.Controller.ControllerContext))
{
if (validator.IsRequired)
{
isRequired = true;
break;
}
}
}
return htmlHelper.CustomLabel(name, labelText, isRequired, withColon, htmlAttributes);
}
}
}