我正在寻找如何扩展Kendo HTML Helpers来执行

@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));
        }
    }
}

关于c# - 为TextBoxFor扩展Kendo HtmlHelpers,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24172261/

10-15 02:31