using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Linq.Expressions; namespace Bowin.Web.Controls.Mvc { public static class RichTextExtensions { public static MvcHtmlString RichText(this HtmlHelper htmlHelper, RichTextOptions richTextOptions, System.Collections.Generic.IDictionary htmlAttributes = null) { return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render()); } public static MvcHtmlString RichTextFor(this HtmlHelper htmlHelper, Expression> expression, RichTextOptions richTextOptions, System.Collections.Generic.IDictionary htmlAttributes = null) { if (expression == null) { throw new ArgumentNullException("expression"); } ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); //获取字段名称,对应标签的name string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); richTextOptions.ID = fullHtmlFieldName; richTextOptions.Name = fullHtmlFieldName; richTextOptions.Value = Convert.ToString(modelMetadata.Model); //MVC验证支持 var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString())); return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render()); } } }