forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlHelper.Input.cs
More file actions
179 lines (149 loc) · 6.64 KB
/
Copy pathHtmlHelper.Input.cs
File metadata and controls
179 lines (149 loc) · 6.64 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Data.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Web.Mvc;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages.Html
{
public partial class HtmlHelper
{
private enum InputType
{
Text,
Password,
Hidden
}
public IHtmlString TextBox(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Text, value: null, isExplicitValue: false,
attributes: (IDictionary<string, object>)null);
}
public IHtmlString TextBox(string name, object value)
{
return TextBox(name, value, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString TextBox(string name, object value, object htmlAttributes)
{
return TextBox(name, value, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString TextBox(string name, object value, IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Text, value, isExplicitValue: true, attributes: htmlAttributes);
}
public IHtmlString Hidden(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Hidden, value: null, isExplicitValue: false,
attributes: (IDictionary<string, object>)null);
}
public IHtmlString Hidden(string name, object value)
{
return Hidden(name, value, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString Hidden(string name, object value, object htmlAttributes)
{
return Hidden(name, value, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString Hidden(string name, object value, IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Hidden, GetHiddenFieldValue(value), isExplicitValue: true,
attributes: htmlAttributes);
}
private static object GetHiddenFieldValue(object value)
{
Binary binaryValue = value as Binary;
if (binaryValue != null)
{
value = binaryValue.ToArray();
}
byte[] byteArrayValue = value as byte[];
if (byteArrayValue != null)
{
value = Convert.ToBase64String(byteArrayValue);
}
return value;
}
public IHtmlString Password(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Password, null, isExplicitValue: false,
attributes: (IDictionary<string, object>)null);
}
public IHtmlString Password(string name, object value)
{
return Password(name, value, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString Password(string name, object value, object htmlAttributes)
{
return Password(name, value, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString Password(string name, object value, IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildInputField(name, InputType.Password, value, isExplicitValue: true, attributes: htmlAttributes);
}
private IHtmlString BuildInputField(string name, InputType type, object value, bool isExplicitValue,
IDictionary<string, object> attributes)
{
TagBuilder tagBuilder = new TagBuilder("input");
// Implicit parameters
tagBuilder.MergeAttribute("type", GetInputTypeString(type));
tagBuilder.GenerateId(name);
// Overwrite implicit
tagBuilder.MergeAttributes(attributes, replaceExisting: true);
if (UnobtrusiveJavaScriptEnabled)
{
// Add validation attributes
var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
tagBuilder.MergeAttributes(validationAttributes, replaceExisting: false);
}
// Function arguments
tagBuilder.MergeAttribute("name", name, replaceExisting: true);
var modelState = ModelState[name];
if ((type != InputType.Password) && modelState != null)
{
// Don't use model values for passwords
value = value ?? modelState.Value ?? String.Empty;
}
if ((type != InputType.Password) || ((type == InputType.Password) && (value != null)))
{
// Review: Do we really need to be this pedantic about sticking to mvc?
tagBuilder.MergeAttribute("value", (string)ConvertTo(value, typeof(string)), replaceExisting: isExplicitValue);
}
AddErrorClass(tagBuilder, name);
return tagBuilder.ToHtmlString(TagRenderMode.SelfClosing);
}
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Input types are specified in lower case")]
private static string GetInputTypeString(InputType inputType)
{
if (!Enum.IsDefined(typeof(InputType), inputType))
{
inputType = InputType.Text;
}
return inputType.ToString().ToLowerInvariant();
}
}
}