为TextBoxFor扩展Kendo HtmlHelpers

我正在寻找如何扩展Kendo HtmlHelpers来做类似的事情

@Html.Kendo().TextBoxFor(model => model.field)

这是我的建议

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Kendo.Mvc.UI.Fluent;

namespace Kendo.Mvc.UI
{
    public static class KendoExtensions
    {
        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            return htmlHelper.TextBoxFor(expression, format: null);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format)
        {
            return htmlHelper.TextBoxFor(expression, format, null);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            return htmlHelper.TextBoxFor(expression, null, htmlAttributes);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, object htmlAttributes)
        {
            return htmlHelper.TextBoxFor(expression, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            return htmlHelper.TextBoxFor(expression, null, htmlAttributes);
        }

        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this WidgetFactory<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, IDictionary<string, object> htmlAttributes)
        {
            var lKWidget = new TagBuilder("span");
            lKWidget.AddCssClass("k-widget k-numerictextbox");

            var lKExpanding = new TagBuilder("span");
            lKExpanding.AddCssClass("k-numeric-wrap k-expand-padding k-state-disabled");

            if (htmlAttributes == null) htmlAttributes = new Dictionary<string, object>();
            if (htmlAttributes.ContainsKey("class"))
            {
                htmlAttributes["class"] += "k-formatted-value k-input";
            } else
            {
                htmlAttributes.Add("class", "k-formatted-value k-input");
            }

            var lTextBoxFor = htmlHelper.HtmlHelper.TextBoxFor(expression, format, htmlAttributes).ToHtmlString();
            lKExpanding.InnerHtml += lTextBoxFor;

            lKWidget.InnerHtml += lKExpanding;

            lKWidget.InnerHtml += htmlHelper.HtmlHelper.ValidationMessageFor(expression);

            return MvcHtmlString.Create(lKWidget.ToString(TagRenderMode.Normal));
        }
    }
}

Kendo UI的新版本(2014.2.716)有这个扩展,还有更多......

链接地址: http://www.djcxy.com/p/79893.html

上一篇: Extend Kendo HtmlHelpers for TextBoxFor

下一篇: rescue In a rake Task?