|
| 1 | +--- |
| 2 | +title: Overview |
| 3 | +page_title: Menu for Blazor | Data Binding Overview |
| 4 | +description: Data Binding basics in the Menu for Blazor |
| 5 | +slug: components/menu/data-binding/overview |
| 6 | +tags: telerik,blazor,menu,data,bind,databind,databinding,basics |
| 7 | +published: True |
| 8 | +position: 0 |
| 9 | +--- |
| 10 | + |
| 11 | +# Menu Data Binding Basics |
| 12 | + |
| 13 | +This article explains the different ways to provide data to a Menu component, the properties related to data binding and their results. |
| 14 | + |
| 15 | +First, review: |
| 16 | + |
| 17 | +* The available (bindable) [features of a menu item](#menu-item-features). |
| 18 | +* How to match fields in the model with the menu item [data bindings](#data-bindings). |
| 19 | + |
| 20 | +There are two modes of providing data to a menu, and they all use the items' features. Once you are familiar with the current article, choose the data binding more you wish to use: |
| 21 | + |
| 22 | +* [Flat data]({%slug components/menu/data-binding/flat-data%}) - a single collection of items with defined parent-child relationships. |
| 23 | +* [Hierarchical data]({%slug components/menu/data-binding/hierarchical-data%}) - separate collections of items and their child items. |
| 24 | + |
| 25 | +## Menu Item Features |
| 26 | + |
| 27 | +The menu items provide the following features that you control through the corresponding fields in their data binding: |
| 28 | + |
| 29 | +* `Id` - a unique identifier for the item. Required for binding to flat data. |
| 30 | +* `ParentId` - identifies the parent to whom the item belongs. Required only when binding to flat data. All items with the same `ParentId` will be rendered at the same level. For a root level item, this must be `null`. |
| 31 | +* `HasChildren` - whether the item has children. Determines whether an expand arrow is rendered next to the item. Required for binding to flat data. With hierarchical data, the menu will render the icon based on the existence of child items, but `HasChildren` will take precedence. |
| 32 | +* `Items` - the collection of child items that will be rendered under the current item. Required only when binding to hierarchical data. |
| 33 | +* `Text` - the text that will be shown on the item. |
| 34 | +* `Icon` / `IconClass` / `ImageUrl` - the [Telerik icon]({%slug general-information/font-icons%}), a class for a custom font icon, or the URL to a raster image that will be rendered in the item. They have the listed order of precedence in case more than one is present in the data (that is, an `Icon` will have the highest importance). |
| 35 | +* `Url` - the view the item will navigate to by generating a link. |
| 36 | + |
| 37 | +## Data Bindings |
| 38 | + |
| 39 | +The properties of a menu item match directly to a field of the model the menu is bound to. You provide that relationship by providing the name of the field from which the corresponding information is present. To do this, use the properties in the main `TelerikMenu` tag: |
| 40 | + |
| 41 | +* IdField => Id |
| 42 | +* ParentIdField => ParentId |
| 43 | +* TextField => Text |
| 44 | +* IconClassField => IconClass |
| 45 | +* IconField => Icon |
| 46 | +* ImageUrlField => ImageUrl |
| 47 | +* UrlField => Url |
| 48 | +* HasChildrenField => HasChildren |
| 49 | +* ItemsField => Items |
| 50 | + |
| 51 | +>tip There are default values for the field names. If your model names match the defaults, you don't have to define them in the bindings settings. |
| 52 | +
|
| 53 | +>caption Default field names for menu item bindings. If you use these, you don't have to specify them in the `TelerikMenu` tag explicitly. |
| 54 | +
|
| 55 | +````CSHTML |
| 56 | +public class MenuItem |
| 57 | +{ |
| 58 | + public int Id { get; set; } |
| 59 | + public string Text { get; set; } |
| 60 | + public int? ParentId { get; set; } |
| 61 | + public bool HasChildren { get; set; } |
| 62 | + public string Icon { get; set; } |
| 63 | + public bool Expanded { get; set; } |
| 64 | + public string Url { get; set; } |
| 65 | +} |
| 66 | +```` |
| 67 | + |
| 68 | +## See Also |
| 69 | + |
| 70 | + * [Binding to Flat Data]({%slug components/menu/data-binding/flat-data%}) |
| 71 | + * [Binding to Hierarchical Data]({%slug components/menu/data-binding/hierarchical-data%}) |
| 72 | + * [Live Demo: Menu Flat Data](https://demos.telerik.com/blazor-ui/menu/flat-data) |
| 73 | + * [Live Demo: Menu Hierarchical Data](https://demos.telerik.com/blazor-ui/menu/hierarchical-data) |
| 74 | + |
0 commit comments