我写了一个自定义的wysiwyg扩展。我注意到,当它生成时,它不包含不显眼的验证
它应该生成这个(但它缺少data-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 wysiwyg扩展名:
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助手呢?会产生我的验证属性吗?
答案 0 :(得分: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()));
...
感谢