为 TextBoxFor 扩展 Kendo HtmlHelpers

Posted

技术标签:

【中文标题】为 TextBoxFor 扩展 Kendo HtmlHelpers【英文标题】:Extend Kendo HtmlHelpers for TextBoxFor 【发布时间】:2014-08-02 01:28:59 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

这是我的建议

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

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:

我把这个和标签一起用

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

【讨论】:

以上是关于为 TextBoxFor 扩展 Kendo HtmlHelpers的主要内容,如果未能解决你的问题,请参考以下文章

TextBoxFor中限制为2位小数

Kendo MVVM 和绑定或扩展自定义事件

你如何扩展 Kendo UI 小部件

TextboxFor 元素和 CheckboxFor 元素为 null 且未发送到 POST

Html.TextBoxFor 中的必需值

扩展 Kendo Angular 2 UI 组件并创建我们自己的具有所有 Kendo 组件功能的组件包装器