forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlHelper.TextArea.cs
More file actions
122 lines (101 loc) · 4.66 KB
/
HtmlHelper.TextArea.cs
File metadata and controls
122 lines (101 loc) · 4.66 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
// 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.Globalization;
using System.Web.Mvc;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages.Html
{
public partial class HtmlHelper
{
// Values from mvc
private const int TextAreaRows = 2;
private const int TextAreaColumns = 20;
private static readonly IDictionary<string, object> _implicitRowsAndColumns = new Dictionary<string, object>
{
{ "rows", TextAreaRows.ToString(CultureInfo.InvariantCulture) },
{ "cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture) },
};
private static IDictionary<string, object> GetRowsAndColumnsDictionary(int rows, int columns)
{
Dictionary<string, object> result = new Dictionary<string, object>();
if (rows > 0)
{
result.Add("rows", rows.ToString(CultureInfo.InvariantCulture));
}
if (columns > 0)
{
result.Add("cols", columns.ToString(CultureInfo.InvariantCulture));
}
return result;
}
public IHtmlString TextArea(string name)
{
return TextArea(name, value: null, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString TextArea(string name, object htmlAttributes)
{
return TextArea(name, value: null, htmlAttributes: AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString TextArea(string name, IDictionary<string, object> htmlAttributes)
{
return TextArea(name, value: null, htmlAttributes: htmlAttributes);
}
public IHtmlString TextArea(string name, string value)
{
return TextArea(name, value, (IDictionary<string, object>)null);
}
public IHtmlString TextArea(string name, string value, object htmlAttributes)
{
return TextArea(name, value, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString TextArea(string name, string value, IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildTextArea(name, value, _implicitRowsAndColumns, htmlAttributes);
}
public IHtmlString TextArea(string name, string value, int rows, int columns,
object htmlAttributes)
{
return TextArea(name, value, rows, columns, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString TextArea(string name, string value, int rows, int columns,
IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildTextArea(name, value, GetRowsAndColumnsDictionary(rows, columns), htmlAttributes);
}
private IHtmlString BuildTextArea(string name, string value, IDictionary<string, object> rowsAndColumnsDictionary,
IDictionary<string, object> htmlAttributes)
{
TagBuilder tagBuilder = new TagBuilder("textarea");
if (UnobtrusiveJavaScriptEnabled)
{
// Add validation attributes
var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
tagBuilder.MergeAttributes(validationAttributes, replaceExisting: false);
}
// Add user specified htmlAttributes
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttributes(rowsAndColumnsDictionary, rowsAndColumnsDictionary != _implicitRowsAndColumns);
// Value becomes the inner html of the textarea element
var modelState = ModelState[name];
if (modelState != null)
{
value = value ?? Convert.ToString(ModelState[name].Value, CultureInfo.CurrentCulture);
}
tagBuilder.InnerHtml = Encode(value);
//Assign name and id
tagBuilder.MergeAttribute("name", name);
tagBuilder.GenerateId(name);
AddErrorClass(tagBuilder, name);
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
}
}
}