2011-03-24 87 views
1

我写了一个自定义wysiwyg扩展。我注意到,当产生它,它不包含不引人注目的验证添加mvc3对自定义TextArea扩展的不显眼验证

应该产生这样(但它缺少数据-VAL ...): <textarea rows="2" name="ContentObject.Description" id="ContentObject_Description" 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>

这是我的textarea所见即所得扩展:

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()); 
     } 

我认为这个问题是因为我使用“标签生成器”。我将如何扩展textarea html helper?会产生我的验证属性?

+0

是验证反正工作或你的,你不能让jQuery验证发挥好与TinyMCE的主要问题? – 2011-03-24 10:42:36

回答

0

我想通了。我这样做这一翻译使用“tagbuilder”的:

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

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

感谢