开发者

add mvc3 Unobtrusive validation to custom TextArea extension

开发者 https://www.devze.com 2023-02-19 05:16 出处:网络
I wrote a custom wysiwyg extension. I noticed that when it is generated it does not contain unobtrusive validation

I wrote a custom wysiwyg extension. I noticed that when it is generated it does not contain unobtrusive validation

it should be generating this (but it's missing data-val...): <textarea rows="2" name="ContentObject.Description" id="ContentObject_Descr开发者_JAVA百科iption" data-val-required="Required field" data-val-length-min="150" data-val-length-max="1000" data-val-length="you need 100 characters" data-val="true" cols="20"></textarea>

this is my textarea wysiwyg extension:

public static MvcHtmlString WysiwygHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, bool isAdvanced, IDictionary<string, object> htmlAttributes)
        {
            var modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
            var metadata = ModelMetadata.FromStringExpression(expression, htmlHelper.ViewData);
            var labelText = metadata.DisplayName ?? metadata.PropertyName ?? modelName.Split('.').Last();
            if (String.IsNullOrEmpty(labelText))
            {
                return MvcHtmlString.Empty;
            }

            var tag = new TagBuilder("textarea");
            tag.MergeAttribute("name", modelName, true);
            tag.GenerateId(modelName);
            tag.MergeAttributes(htmlAttributes);

            // If there are any errors for a named field, we add the CSS attribute.
            ModelState modelState;
            if (htmlHelper.ViewData.ModelState.TryGetValue(modelName, out modelState) && modelState.Errors.Count > 0)
            {
                tag.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }

            string value;
            if (modelState != null && modelState.Value != null)
            {
                value = modelState.Value.AttemptedValue;
            }
            else if (modelMetadata.Model != null)
            {
                value = modelMetadata.Model.ToString();
            }
            else
            {
                value = String.Empty;
            }

            // The first newline is always trimmed when a TextArea is rendered, so we add an extra one
            // in case the value being rendered is something like "\r\nHello".
            tag.SetInnerText(Environment.NewLine + value);

            htmlHelper.EnableUnobtrusiveJavaScript( )

            // TinyMCE script
            var sb = new StringBuilder();

            sb.Append(MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)));
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("$(document).ready(function () {");
            sb.AppendLine("$(\"#" + htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(expression) + "\").tinymce({");
            sb.AppendLine("script_url: '/Scripts/tiny_mce/tiny_mce.js',");
            sb.AppendLine("theme: \"advanced\",");
            sb.AppendLine("theme_advanced_toolbar_location : \"top\",");
            sb.AppendLine("plugins: \"style,table,advlink,preview,paste,fullscreen\",");
            if (isAdvanced)
            {
                sb.AppendLine(" theme_advanced_buttons1: \"bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,removeformat,fullscreen,preview\",");
                sb.AppendLine(" theme_advanced_buttons2 : \"\",");
                sb.AppendLine(" theme_advanced_buttons3 : \"\",");
                sb.AppendLine(" theme_advanced_toolbar_align: \"left\",");
                sb.AppendLine(" theme_advanced_statusbar_location: \"bottom\",");
                sb.AppendLine(" theme_advanced_resizing: false,");
            }
            else
            {
                sb.AppendLine("theme_advanced_buttons1 : \"bold,italic,underline,strikethrough,|,undo,redo,|,bullist,numlist\",");
                sb.AppendLine("theme_advanced_buttons2 : \"\",");
                sb.AppendLine("theme_advanced_buttons3 : \"\",");
                sb.AppendLine("theme_advanced_buttons4 : \"\",");
            }
            sb.AppendLine(" content_css: \"/Content/style.css\"");
            sb.AppendLine("});");
            sb.AppendLine("});");
            sb.AppendLine("</script>");

            return MvcHtmlString.Create(sb.ToString());
        }

I think the problem is because i'm using "tag builder". how would i do it extending textarea html helper? would that generate my validation attributes?


I figured it out. I did this intead of using "tagbuilder":

...
            // Create textarea html element
            var htmlElement = htmlHelper.TextArea(modelName, value, htmlAttributes);

            // TinyMCE script
            var sb = new StringBuilder();
            sb.Append(MvcHtmlString.Create(htmlElement.ToHtmlString()));
...

thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号