|
| 1 | +--- |
| 2 | +title: Data Binding |
| 3 | +page_title: DropDown List for Blazor | Data Binding |
| 4 | +description: Data Binding the DropdownList for Blazor |
| 5 | +slug: components/dropdownlist/databind |
| 6 | +tags: telerik,blazor,dropdownlist,dropdown,list,data,bind,binding,databind |
| 7 | +published: True |
| 8 | +position: 1 |
| 9 | +--- |
| 10 | + |
| 11 | +# DropDownList Data Binding |
| 12 | + |
| 13 | +This article explains the different ways to provide data to a DropDownList component, the properties related to data binding and their results. |
| 14 | + |
| 15 | +There are two key ways to bind data: |
| 16 | + |
| 17 | +* [Primitive Types](#primitive-types) |
| 18 | +* [Model](#bind-to-a-model) |
| 19 | + |
| 20 | +There are also some [considerations](#considerations) to keep in mind. |
| 21 | + |
| 22 | +## Primitive Types |
| 23 | + |
| 24 | +You can data bind the DropDownList to a simple collection of data. When you have a concrete list of options for the user to choose from, their string representation is often suitable for display and you do not need special models. |
| 25 | + |
| 26 | +To bind the dropdownlist to a primitive type (like `int`, `string`, `double`), you need to |
| 27 | + |
| 28 | +1. provide an `IEnumerable<TItem>` of the desired type to its `Data` property |
| 29 | +1. set a corresponding `Value`. If the `Value` is `null`, it will be populated with the first item from the data source. |
| 30 | + |
| 31 | +>caption Data binding a DropDownList to a primitive type |
| 32 | +
|
| 33 | +````CSHTML |
| 34 | +@using Telerik.Blazor.Components.DropDownList |
| 35 | +
|
| 36 | +<TelerikDropDownList Data="@MyList" @bind-Value="MyItem"> |
| 37 | +</TelerikDropDownList> |
| 38 | +
|
| 39 | +@code { |
| 40 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 41 | +
|
| 42 | + protected string MyItem { get; set; } = "second"; |
| 43 | +} |
| 44 | +```` |
| 45 | + |
| 46 | +## Bind to a Model |
| 47 | + |
| 48 | +You can bind the DropDownList to a model in your application. This is useful when you have a numerical representation of a finite list (for example, departments in a company), and you want the user to choose them based on a friendly text name. |
| 49 | + |
| 50 | +To bind the DropDownList to a model: |
| 51 | + |
| 52 | +1. populate its `Data` property with the collection of items you want in the dropdown |
| 53 | +1. set the `TextField` and `ValueField` properties to point to the corresponding names of the model |
| 54 | +1. set the `Value` property to the intial value of the model. If not set, it will be populated with the first item in the data source. |
| 55 | + |
| 56 | +>caption Data binding a DropDownList to a model |
| 57 | +
|
| 58 | +````CSHTML |
| 59 | +@using Telerik.Blazor.Components.DropDownList |
| 60 | +
|
| 61 | +<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="selectedValue"> |
| 62 | +</TelerikDropDownList> |
| 63 | +
|
| 64 | +@code { |
| 65 | + //in a real case, the model is usually in a separate file |
| 66 | + //the model type and value field type must be provided to the dropdpownlist |
| 67 | + public class MyDdlModel |
| 68 | + { |
| 69 | + public int MyValueField { get; set; } |
| 70 | + public string MyTextField { get; set; } |
| 71 | + } |
| 72 | +
|
| 73 | + IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }); |
| 74 | +
|
| 75 | + int selectedValue { get; set; } = 3; //usually the current value should come from the view model data |
| 76 | +} |
| 77 | +```` |
| 78 | + |
| 79 | +## Considerations |
| 80 | + |
| 81 | +The DropDownList component attempts to infer the type of its model and value based on the provided `Data` and initial `Value`. This affects the way its [reference is obtained](#component-reference) and what happens [if you can't provide data or a value](#missing-value-or-data). |
| 82 | + |
| 83 | +### Component Reference |
| 84 | + |
| 85 | +The DropDownList is a generic component and its type comes from the model it is bound to and from the value field type. When bound to a primitive type, the reference is of that primitive type only. |
| 86 | + |
| 87 | +````Primitive |
| 88 | +@using Telerik.Blazor.Components.DropDownList |
| 89 | +
|
| 90 | +<TelerikDropDownList @ref="myDdlRef" Data="@MyList" Value="third"> |
| 91 | +</TelerikDropDownList> |
| 92 | +
|
| 93 | +@code { |
| 94 | + //the type of the generic component is determined by the type of the model you pass to it, and the type of its value field |
| 95 | + Telerik.Blazor.Components.DropDownList.TelerikDropDownList<string, string> myDdlRef; |
| 96 | +
|
| 97 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 98 | +} |
| 99 | +```` |
| 100 | +````Model |
| 101 | +@using Telerik.Blazor.Components.DropDownList |
| 102 | +
|
| 103 | +<TelerikDropDownList @ref="myDdlRef" Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" Value="3"> |
| 104 | +</TelerikDropDownList> |
| 105 | +@code { |
| 106 | + //the type of the generic component is determined by the type of the model you pass to it, and the type of its value field |
| 107 | + Telerik.Blazor.Components.DropDownList.TelerikDropDownList<MyDdlModel, int> myDdlRef; |
| 108 | +
|
| 109 | + IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }); |
| 110 | + |
| 111 | + public class MyDdlModel |
| 112 | + { |
| 113 | + public int MyValueField { get; set; } |
| 114 | + public string MyTextField { get; set; } |
| 115 | + } |
| 116 | +} |
| 117 | +```` |
| 118 | + |
| 119 | +### Missing Value or Data |
| 120 | + |
| 121 | + In case you cannot provide either of a `Value`, or `Data`, or both when the component initializes, you need to set the corresponding type properties to the `TItem` and `TValue` properties as shown below. |
| 122 | + |
| 123 | +>caption DropDownList configuration if you cannot provide Value or Data |
| 124 | +
|
| 125 | +````CSHTML |
| 126 | +@using Telerik.Blazor.Components.DropDownList |
| 127 | +
|
| 128 | +<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" TValue="int" TItem="MyDdlModel"> |
| 129 | +</TelerikDropDownList> |
| 130 | +
|
| 131 | +@code { |
| 132 | + public class MyDdlModel //TItem matches the type of the model |
| 133 | + { |
| 134 | + public int MyValueField { get; set; } //TValue matches the type of the value field |
| 135 | + public string MyTextField { get; set; } |
| 136 | + } |
| 137 | +
|
| 138 | + IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x }); |
| 139 | + |
| 140 | + //the same configuration applies if the "myDdlData" object is null initially and is populated on some event |
| 141 | +} |
| 142 | +```` |
| 143 | + |
| 144 | + |
| 145 | +## See Also |
| 146 | + |
| 147 | + * [Live Demo: DropDownList](https://demos.telerik.com/blazor-ui/dropdownlist/index) |
| 148 | + * [Live Demo: DropDownList Validation](https://demos.telerik.com/blazor-ui/dropdownlist/validation) |
| 149 | + |
| 150 | + |
0 commit comments