RichTextExtensions.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public static class RichTextExtensions
  10. {
  11. public static MvcHtmlString RichText(this HtmlHelper htmlHelper,
  12. RichTextOptions richTextOptions,
  13. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  14. {
  15. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render());
  16. }
  17. public static MvcHtmlString RichTextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
  18. Expression<Func<TModel, TProperty>> expression,
  19. RichTextOptions richTextOptions,
  20. System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
  21. {
  22. if (expression == null)
  23. {
  24. throw new ArgumentNullException("expression");
  25. }
  26. ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
  27. //获取字段名称,对应标签的name
  28. string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
  29. richTextOptions.ID = fullHtmlFieldName;
  30. richTextOptions.Name = fullHtmlFieldName;
  31. richTextOptions.Value = Convert.ToString(modelMetadata.Model);
  32. //MVC验证支持
  33. var validateAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, modelMetadata);
  34. if (htmlAttributes == null)
  35. {
  36. htmlAttributes = new Dictionary<string, string>();
  37. }
  38. validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
  39. return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render());
  40. }
  41. }
  42. }