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

Handle Unique Identifiers with Menu Items

Environment

ProductTelerik UI for ASP.NET Core Menu
Product VersionCreated with version 2024.4.1112

Description

How can I set custom identifiers to the Telerik UI for ASP.NET Core Menu items and implement custom logic when the respective item is clicked?

Solution

Commonly, you are required to handle custom identifiers with click handlers or, as in this case, by using the client-side Select event of the component.

To provide for the functionality, the Menu mainly uses the HTML elements that are rendered, so you can use custom HTML attributes to serve as unique identifiers.

In this way, you can programmatically:

  • Identify the clicked item.
  • Utilize values that are specific to the item other than text.
  • Remove a specific item from the Menu by identifying it through an attribute.
  • Run a custom logic for particular items.

The following example demonstrates how to handle a custom command attribute to accomplish specific application requirements, such as the ones mentioned in the above list.

Razor
    @(Html.Kendo().Menu()
        .Name("menu")
        .Items(items => {
            items.Add()
                .Text("Command 1")
                .HtmlAttributes(new { data_command = "command1" });

            items.Add()
                .Text("Command 2")
                .HtmlAttributes(new { data_command = "command2" });

            items.Add()
                .Text("Remove Command 2")
                .HtmlAttributes(new { @class = "remove-command", data_removecommand = "command2" });
        })
        .Events(events => events.Select("onSelect"))
    )

More ASP.NET Core Menu Resources

See Also