using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace Kreta.Web.Helpers { public static class TextareaExtensions { public static MvcHtmlString KretaTextArea(this HtmlHelper helper, string name, List value, int rows = 3, Dictionary htmlAttributes = null) { StringBuilder textareaSB = new StringBuilder(); textareaSB.Append("", HttpUtility.HtmlEncode(string.Join("\n", value))); return new MvcHtmlString(textareaSB.ToString()); } public static MvcHtmlString KretaTextAreaFor(this HtmlHelper helper, Expression> expression, int rows = 3, Dictionary htmlAttributes = null) { var fieldName = ExpressionHelper.GetExpressionText(expression); var fullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName); var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); var value = metadata.Model; if (htmlAttributes == null) htmlAttributes = new Dictionary(); var attributes = Mapper.GetUnobtrusiveValidationAttributes(helper, expression, htmlAttributes, metadata); foreach (var key in attributes.Keys) { if (!htmlAttributes.ContainsKey(key)) { htmlAttributes.Add(key, attributes[key].ToString()); } } var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata); if (metadata.IsRequired) { htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *"); } else { if (metadata.DisplayName == null) { htmlAttributes.Add("data-labelmsg", ""); } else { htmlAttributes.Add("data-labelmsg", metadata.DisplayName); } } if (htmlAttributes.ContainsKey("class")) { htmlAttributes["class"] += " k-textbox"; } else { htmlAttributes.Add("class", "k-textbox"); } if (expression.Body is MemberExpression me && me.Member != null) { int maxLength = 0; MaxLengthAttribute maxLengthAttribute = me.Member.GetCustomAttributes(typeof(MaxLengthAttribute), false).Cast().FirstOrDefault(); if (maxLengthAttribute != null) { maxLength = maxLengthAttribute.Length; } else { StringLengthAttribute stringLengthAttribute = me.Member.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast().FirstOrDefault(); if (stringLengthAttribute != null) { maxLength = stringLengthAttribute.MaximumLength; } } if (maxLength > 0) { htmlAttributes["maxlength"] = maxLength.ToString(); } } StringBuilder textareaSB = new StringBuilder(); textareaSB.Append(""); return new MvcHtmlString(textareaSB.ToString()); } public static MvcHtmlString RenderWithName(this MvcHtmlString helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string customLabelDivClass = "", string customLabelClass = "", string tooltipResource = null) { StringBuilder sb = new StringBuilder(); /*Hack hogy kiszedjük a stringből a labelMsg-t*/ int startIndex = helper.ToString().IndexOf("data-labelmsg"); string str = helper.ToString().Substring(startIndex + 15).Replace("", ""); int startIndex2 = str.IndexOf("'"); string labelMsg = str.Substring(0, startIndex2); /*Hack hogy kiszedjük a stringből a labelMsg-t*/ /*Hack hogy kiszedjük a stringből az Id-t*/ int startIndexId = helper.ToString().IndexOf("id"); int startIndexName = helper.ToString().IndexOf("name"); string str2 = helper.ToString().Substring(0, startIndexName).Replace("