New to Telerik UI for ASP.NET MVCStart a free 30-day trial

Defining Custom HtmlHelpers

Environment

ProductTelerik UI for ASP.NET MVC
Product VersionCreated with version 2024.4.1112

Description

How can I define a custom HtmlHelper that accepts additional configuration options by extending an arbitrary Telerik UI for ASP.NET MVC component?

Solution

This example shows how to define a custom HtmlHelper named MyGrid by extending the Grid component.

  1. Define a static Extension Method to extend the Grid configuration.
  2. Add the desired options to the method and return the extended type.
  3. From there, reference the extension method within the page where the component will be utilized.
MyGridHelper.cs
    namespace Telerik.Examples.Mvc.Areas.DefineCustomHtmlHelper.Helpers
    {
        public static class MyGridHelper
        {
            public static Kendo.Mvc.UI.Fluent.GridBuilder<T> MyGrid<T>(this HtmlHelper helper, string name)
                where T : class
            {
                return helper.Kendo().Grid<T>()
                    .Name(name)
                    .Groupable()
                    .Sortable()
                    .Scrollable()
                    .Filterable()
                    .Pageable();
            }
        }
    }

For a runnable example, refer to the ASP.NET MVC application in the UI for ASP.NET MVC Examples repository.

More ASP.NET MVC Grid Resources

See Also