1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Html {
\r
15 using System.Collections.Generic;
\r
16 using System.Globalization;
\r
17 using System.Web.Mvc.Resources;
\r
18 using System.Web.Routing;
\r
20 public static class TextAreaExtensions {
\r
21 // These values are similar to the defaults used by WebForms
\r
22 // when using <asp:TextBox TextMode="MultiLine"> without specifying
\r
23 // the Rows and Columns attributes.
\r
24 private const int TextAreaRows = 2;
\r
25 private const int TextAreaColumns = 20;
\r
27 public static string TextArea(this HtmlHelper htmlHelper, string name) {
\r
28 return TextArea(htmlHelper, name, (object)null /* htmlAttributes */);
\r
31 public static string TextArea(this HtmlHelper htmlHelper, string name, object htmlAttributes) {
\r
32 return TextArea(htmlHelper, name, new RouteValueDictionary(htmlAttributes));
\r
35 public static string TextArea(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {
\r
36 // Add implicit parameters
\r
37 Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();
\r
38 implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));
\r
39 implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));
\r
40 return TextAreaHelper(htmlHelper, name, true /* useViewData */, null /* value */, implicitAttributes, null /* explicitParameters */, htmlAttributes);
\r
43 public static string TextArea(this HtmlHelper htmlHelper, string name, string value) {
\r
44 return TextArea(htmlHelper, name, value, (object) null /* htmlAttributes */);
\r
47 public static string TextArea(this HtmlHelper htmlHelper, string name, string value, object htmlAttributes) {
\r
48 return TextArea(htmlHelper, name, value, new RouteValueDictionary(htmlAttributes));
\r
51 public static string TextArea(this HtmlHelper htmlHelper, string name, string value, IDictionary<string, object> htmlAttributes) {
\r
52 // Add implicit parameters
\r
53 Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();
\r
54 implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));
\r
55 implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));
\r
56 return TextAreaHelper(htmlHelper, name, (value == null) /* useViewData */, value, implicitAttributes, null /* explicitParameters */, htmlAttributes);
\r
59 public static string TextArea(this HtmlHelper htmlHelper, string name, string value, int rows, int columns, object htmlAttributes) {
\r
60 return TextArea(htmlHelper, name, value, rows, columns, new RouteValueDictionary(htmlAttributes));
\r
63 public static string TextArea(this HtmlHelper htmlHelper, string name, string value, int rows, int columns, IDictionary<string, object> htmlAttributes) {
\r
65 throw new ArgumentOutOfRangeException("rows", MvcResources.HtmlHelper_TextAreaParameterOutOfRange);
\r
68 throw new ArgumentOutOfRangeException("columns", MvcResources.HtmlHelper_TextAreaParameterOutOfRange);
\r
71 Dictionary<string, object> explicitParameters = new Dictionary<string, object>();
\r
72 explicitParameters.Add("rows", rows.ToString(CultureInfo.InvariantCulture));
\r
73 explicitParameters.Add("cols", columns.ToString(CultureInfo.InvariantCulture));
\r
74 return TextAreaHelper(htmlHelper, name, (value == null) /* useViewData */, value, null /* implictAttributes */, explicitParameters, htmlAttributes);
\r
77 private static string TextAreaHelper(this HtmlHelper htmlHelper, string name, bool useViewData, string value, IDictionary<string, object> implicitAttributes, IDictionary<string, object> explicitParameters, IDictionary<string, object> htmlAttributes) {
\r
78 if (String.IsNullOrEmpty(name)) {
\r
79 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
\r
82 TagBuilder tagBuilder = new TagBuilder("textarea");
\r
83 // Add implicit attributes.
\r
84 tagBuilder.MergeAttributes(implicitAttributes);
\r
85 tagBuilder.GenerateId(name);
\r
86 // Merge htmlAttributes.
\r
87 tagBuilder.MergeAttributes(htmlAttributes, true);
\r
88 // Override all the attributes with explicit parameters.
\r
89 tagBuilder.MergeAttributes(explicitParameters, true);
\r
90 tagBuilder.MergeAttribute("name", name, true);
\r
92 // If there are any errors for a named field, we add the css attribute.
\r
93 ModelState modelState;
\r
94 if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) {
\r
95 if (modelState.Errors.Count > 0) {
\r
96 tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
\r
100 // The first newline is always trimmed when a TextArea is rendered, so we add an extra one
\r
101 // in case the value being rendered is something like "\r\nHello".
\r
102 // The attempted value receives precedence over the explicitly supplied value parameter.
\r
103 string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
\r
104 tagBuilder.SetInnerText(Environment.NewLine + (attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : value)));
\r
105 return tagBuilder.ToString(TagRenderMode.Normal);
\r