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

Defining Custom HtmlHelper Methods

Environment

ProductTelerik UI for ASP.NET Core
Progress Telerik UI for ASP.NET Core versionCreated with the 2023.1.117 version

Description

How can I define my own custom HtmlHelper methods for an arbitrary Telerik UI for ASP.NET Core component?

Solution

To achieve the desired result:

  1. Create a static Extension Method to extend the desired type for the utilized configuration of the component.
  2. Within the method, implement the desired logic and return the extended type.
  3. From there, reference the extension method within the page in which it will be utilized.
  4. Finally, invoke the defined extension method inside the component's configuration.

The following example illustrates how to define a custom HtmlHelper method for the Grid:

Index.cshtml
    @using TelerikExample.Extensions

    @(Html.Kendo().Grid<OrderViewModel>()
         .Name("grid")
         .Columns(columns =>
         {
             columns.Bound(p => p.Discontinued);
             columns.Bound(p => p.Approved);
         })
         .CustomPageable()
         .DataSource(dataSource => dataSource
             .Ajax()
             .PageSize(20)
             .Read(read => read.Action("Orders_Read", "Grid"))
          )
    )

More ASP.NET Core Grid Resources

See Also