Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Kendo HtmlHelpers for TextBoxFor

I'm looking how to extend Kendo HtmlHelpers to do things like

@Html.Kendo().TextBoxFor(model => model.field)
like image 787
Rodolpho Brock Avatar asked Jun 11 '14 20:06

Rodolpho Brock


People also ask

What are HTML helpers in Kendo UI?

HTML helpers are lightweight objects responsible for generating markup within a view. Kendo UI for ASP.NET Beta renders Web and DataViz widgets through as a set of custom HTML helpers defined in the Kendo.Mvc.UI namespace: Let's start with a simple HTML helper that allows us to examine how they operate.

How do I set the value of a textbox in kendo?

@ (Html.Kendo().TextBox() .Name("textbox") // The name of the TextBox is mandatory. It specifies the "id" attribute of the TextBox. . Value("John Doe") // Set the value of the TextBox. )

What is included in the Kendo UI installer?

The installer will deploy a number of files to the destination folder, including the .NET assembly for Kendo UI for ASP.NET MVC, its associated JavaScript and stylesheet dependencies, documentation, and a set of examples highlighting the HTML helpers of Kendo UI for ASP.NET MVC Beta (more on this later). Important!

What is Telerik UI textbox htmlhelper for MVC?

The Telerik UI TextBox HtmlHelper for ASP.NET MVC is a server-side wrapper for the Kendo UI TextBox widget. The TextBox provides a set of default API configuration options that can be set during its initialization such as value, placeholder, and so on. The following example demonstrates the basic configuration for the TextBox.


1 Answers

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));
        }
    }
}
like image 187
Rodolpho Brock Avatar answered Sep 25 '22 01:09

Rodolpho Brock