public static MvcHtmlString TextAreaWithMaxLengthFor <TViewModel, TValue>(this HtmlHelper <TViewModel> html, Expression <Func <TViewModel, TValue> > expression, TextAreaDimensions textAreaDimensions , string optionalPlaceholderText , IEnumerable <string> cssClasses ) { return(html.TextAreaWithMaxLengthFor(expression, textAreaDimensions, optionalPlaceholderText, cssClasses, true)); }
/// <summary> /// Build style string for the TextArea. /// </summary> public static string BuildStyleString(TextAreaDimensions textAreaDimensions, TextAreaEnableType textAreaEnableType) { const string disabledBackgroundColor = "#DDDDDD"; var backgroundColorString = textAreaEnableType == TextAreaEnableType.Enabled ? string.Empty : " background-color: " + disabledBackgroundColor; // We put in a "width" field (and not the "cols" attribute) when we are *NOT* in an IE browser AND we aren't using a monospaced font var pixelWidthString = textAreaDimensions.ColumnWidthInPixels.HasValue ? $"width:{textAreaDimensions.ColumnWidthInPixels}px" : "width:100%"; const string disableResizeString = "resize: none"; var styleString = $"{backgroundColorString};{pixelWidthString};{disableResizeString};"; return(styleString); }
/// <summary> /// Custom TextArea control that has the max chars left in another div /// Only public for unit testing /// </summary> public static MvcHtmlString TextAreaWithMaxLengthFor <TViewModel, TValue>(this HtmlHelper <TViewModel> html, Expression <Func <TViewModel, TValue> > expression, TextAreaDimensions textAreaDimensions, string optionalPlaceholderText, IEnumerable <string> cssClasses) { int?maxLength = null; var memberExpression = expression.Body as MemberExpression; if (memberExpression != null) { var stringLengthAttribute = memberExpression.Member.GetCustomAttributes(typeof(StringLengthAttribute), true) .Cast <StringLengthAttribute>() .SingleOrDefault(); if (stringLengthAttribute != null) { maxLength = stringLengthAttribute.MaximumLength; } } var textAreaEnableType = TextAreaEnableType.Enabled; var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var value = (string)metadata.Model; var fieldName = ExpressionHelper.GetExpressionText(expression); var fullBindingName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName); var fieldId = TagBuilder.CreateSanitizedId(fullBindingName); var textAreaTag = new TagBuilder("textarea"); if (cssClasses != null) { foreach (var cssClass in cssClasses) { textAreaTag.AddCssClass(cssClass); } } textAreaTag.Attributes.Add("name", fullBindingName); textAreaTag.Attributes.Add("id", fieldId); if (textAreaEnableType == TextAreaEnableType.Disabled) { textAreaTag.Attributes.Add("readonly", "readonly"); } textAreaTag.Attributes.Add("style", BuildStyleString(textAreaDimensions, textAreaEnableType)); if (!string.IsNullOrWhiteSpace(optionalPlaceholderText)) { textAreaTag.Attributes.Add("placeholder", optionalPlaceholderText); } textAreaTag.Attributes.Add("rows", textAreaDimensions.Rows.ToString(CultureInfo.InvariantCulture)); textAreaTag.InnerHtml = value; var validationAttributes = html.GetUnobtrusiveValidationAttributes(fullBindingName, metadata); foreach (var key in validationAttributes.Keys) { textAreaTag.Attributes.Add(key, validationAttributes[key].ToString()); } // For varchar(max) consider -1 is the same as *unlimited* if (maxLength.HasValue && maxLength > 0) { return(AddCharactersRemainingToTextArea(textAreaTag, fieldId, value, maxLength.Value, textAreaDimensions.ColumnWidthInPixels.HasValue)); } return(new MvcHtmlString(textAreaTag.ToString(TagRenderMode.Normal))); }
/// <summary> /// Custom TextArea control that has the max chars left in another div /// Only public for unit testing /// </summary> public static MvcHtmlString TextAreaWithMaxLengthFor <TViewModel, TValue>(this HtmlHelper <TViewModel> html, Expression <Func <TViewModel, TValue> > expression, TextAreaDimensions textAreaDimensions, string optionalPlaceholderText) { return(html.TextAreaWithMaxLengthFor(expression, textAreaDimensions, optionalPlaceholderText, null)); }