为TextBoxFor扩展Kendo HtmlHelpers

时间:2014-06-11 20:45:12

标签: c# asp.net-mvc asp.net-mvc-4 kendo-ui

我正在寻找如何扩展Kendo HtmlHelpers来做像

这样的事情
@Html.Kendo().TextBoxFor(model => model.field)

3 个答案:

答案 0 :(得分:4)

这是我的建议

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

答案 1 :(得分:0)

新版Kendo UI(2014.2.716)有此扩展,还有更多......

答案 2 :(得分:0)

我将其与标签一起使用

     @Html.EnhancedEditorField(model => model.Title)

在InputExtensions类中

public static class InputExtensions
{
    public const string noteTemplate = @"<span class=""inputNote"">{0}</span>";
    public const string fieldClosing = @"</tr>";
    public const string fieldOpenning = @"<tr>";
    public const string fieldContent =
        @"<td class=""labelWrp""> {0} </td>     <td class=""inputWrp"" colspan=""{4}""> {1} <br /> {2} {3} </td>";
    public static MvcHtmlString EnhancedEditorField<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,
                Expression<Func<TModel, TValue>> expression,
                object htmlAttributes = null, string note = null)
    {
        return EnhancedField(htmlHelper, expression, htmlHelper.EditorFor(expression, htmlAttributes), note);
    }

    private static MvcHtmlString EnhancedField<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,
               Expression<Func<TModel, TValue>> expression, MvcHtmlString inputString,
               string note = null, FieldGroupingSetting fieldGroupingSetting = FieldGroupingSetting.CloseBoth, bool checkIsRequired = true)
    {
        MvcHtmlString labelString = htmlHelper.EnhancedLabelFor(expression, checkIsRequired: checkIsRequired);
        MvcHtmlString validationMessageString = htmlHelper.ValidationMessageFor(expression);

        return CreateField(inputString.ToString(), note, labelString.ToString(), validationMessageString.ToString(),
            fieldGroupingSetting);
    }

    private static MvcHtmlString CreateField(string inputString, string note, string labelString,
                string validationMessageString,
                FieldGroupingSetting fieldGroupingSetting = FieldGroupingSetting.CloseBoth, int valueCellCount = 1)
    {
        string noteString = string.Empty;
        if (!string.IsNullOrWhiteSpace(note))
            noteString = string.Format(noteTemplate, note);

        string fieldTemplate =
            (fieldGroupingSetting.HasFlag(FieldGroupingSetting.CloseStart) ? fieldOpenning : string.Empty) +
            fieldContent +
            (fieldGroupingSetting.HasFlag(FieldGroupingSetting.CloseEnd) ? fieldClosing : string.Empty);

        string htmlString = string.Format(fieldTemplate, labelString, inputString, validationMessageString,
            noteString, valueCellCount);
        return MvcHtmlString.Create(htmlString);
    }
}