Extend Kendo HtmlHelpers for TextBoxFor

I am looking for how to extend Kendo HtmlHelpers to do something like

@Html.Kendo().TextBoxFor(model => model.field) 
+7
c # asp.net-mvc asp.net-mvc-4 kendo-ui
source share
2 answers

The new version of the Kendo UI interface (2014.2.716) has this extension and much more ...

0
source share

This is my suggestion.

 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)); } } } 
+4
source share

All Articles