Getting Started with the Breadcrumb
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC Breadcrumb and highlights the major steps in the configuration of the component.
You will initialize a Breadcrumb control, configure its icons, and handle its events.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET MVC HtmlHelpers:
@using Kendo.Mvc.UI
2. Initialize the Breadcrumb
Use the Breadcrumb HtmlHelper to add the component to a page. The Name()
configuration method is mandatory as its value is used for the id
and the name attributes of the Breadcrumb element.
@using Kendo.Mvc.UI
@(Html.Kendo().Breadcrumb()
.Name("breadcrumb")
)
3. Add Breadcrumb Items
The next step is to use the Items()
configuration and add the Breadcrumb items. To configure the items:
Use the
Type()
property to define the type of the added item—RootItem
orItem
.Use the
Text()
property to define the textual content for the Breadcrumb item.Use the
Href()
property to set the navigation URL of the item.-
Use the
ShowText()
property to render the textual content for theRootItem
:- The default value for
RootItem
isfalse
. - The default value for
Item
istrue
.
- The default value for
@(Html.Kendo().Breadcrumb()
.Name("breadcrumb")
.Items(items =>
{
items.Add()
.Type(BreadcrumbItemType.RootItem)
.Href("https://demos.telerik.com/aspnet-core/")
.Text("All Components")
.ShowText(true);
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/breadcrumb")
.Text("Breadcrumb");
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/events")
.Text("Events");
})
)
4. Add Icons
You can configure the icons for the Breadcrumb items by using the Icon()
property. By default, the Breadcrumb renders an icon only for the RootItem
type. To display icons for the Item
type, set the ShowIcon()
property to true
. Additionally, you can also change the icons that separate the Breadcrumb items.
@(Html.Kendo().Breadcrumb()
.Name("breadcrumb")
.Items(items =>
{
items.Add()
.Type(BreadcrumbItemType.RootItem)
.Href("https://demos.telerik.com/aspnet-core/")
.Text("All Components")
.ShowText(true)
.Icon("globe");
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/breadcrumb")
.Text("Breadcrumb")
.Icon("gear")
.ShowIcon(true);
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/events")
.Text("Events")
.Icon("cloud")
.ShowIcon(true);
})
)
5. Handle Breadcrumb Events
The Breadcrumb component exposes the Change()
and Click()
events that you can handle and further customize the functionality of the component.
@(Html.Kendo().Breadcrumb()
.Name("breadcrumb")
.Events(e =>
e.Click("onClick")
.Change("onChange"))
.Items(items =>
{
items.Add()
.Type(BreadcrumbItemType.RootItem)
.Href("https://demos.telerik.com/aspnet-core/")
.Text("All Components")
.ShowText(true)
.Icon("globe");
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/breadcrumb")
.Text("Breadcrumb")
.Icon("gear")
.ShowIcon(true);
items.Add()
.Type(BreadcrumbItemType.Item)
.Href("/events")
.Text("Events")
.Icon("cloud")
.ShowIcon(true);
})
)
<script>
function onClick(e) {
console.log("Clicked. :: target: " + e.item.text + ". Type :: " + e.item.type);
}
function onChange(e) {
console.log("Changed. New Value :: " + e.value);
}
</script>
For more examples, refer to the demo on using the events of the Breadcrumb.
6. (Optional) Reference Existing Breadcrumb Instances
You can reference the Breadcrumb instances that you have created and build on top of their existing configuration:
-
Use the
id
attribute of the component instance to establish a reference.<script> var breadcrumbReference = $("#breadcrumb").data("kendoBreadcrumb"); // breadcrumbReference is a reference to the existing instance of the helper. </script>
-
Use the Breadcrumb client-side API to control the behavior of the widget. In this example, you will use the
value
method to change Breadcrumb value.<script> var breadcrumbReference = $("#breadcrumb").data("kendoBreadcrumb"); // breadcrumbReference is a reference to the existing instance of the helper. breadcrumbReference.value("All Components/Breadcrumb/API"); // Set a new value for the component. </script>
For more information on referencing specific helper instances, see the Methods and Events article.