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

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

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

【问题讨论】:

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


    【解决方案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);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          • 1970-01-01
          • 2013-07-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多