1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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<string, string> htmlAttributes = null)
- {
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render());
- }
- public static MvcHtmlString RichTextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
- Expression<Func<TModel, TProperty>> expression,
- RichTextOptions richTextOptions,
- System.Collections.Generic.IDictionary<string, string> htmlAttributes = null)
- {
- if (expression == null)
- {
- throw new ArgumentNullException("expression");
- }
- ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(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<string, string>();
- }
- validateAttributes.ToList().ForEach(x => htmlAttributes.Add(x.Key, x.Value.ToString()));
- return MvcHtmlString.Create(Bowin.Web.Controls.Mvc.RichText.CreateControl(richTextOptions, htmlAttributes).Render());
- }
- }
- }
|