# DropDownList Data Binding

This article explains the different ways to provide data to a DropDownList component, the properties related to data binding and their results.

For details on Value Binding and Data Binding, and the differences between them, see the [Data Binding vs Value Binding](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/overview#data-binding-vs-value-binding) section.

There are two key ways to bind data:

* [Bind to Strings or Value Types](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#strings-or-value-types)
* [Bind to a Model](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#bind-to-a-model)

There are also some considerations you may find useful, such as showing the `DefaultText` when the value is out of the data source range:

* [Considerations](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#considerations)
  * [Value Out of Range](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#value-out-of-range)
  * [Missing Value or Data](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#missing-value-or-data)

## Strings or Value Types

You can data bind the DropDownList to a collection of `string` or [value type](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types) data (such as `int`, `decimal`, `bool`, and `Enum`). 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.

1. Provide an `IEnumerable<TItem>` of the desired type to its `Data` property.
2. Set a corresponding `Value`. If the `Value` is `null`, it will be populated with the first item from the data source.

> caption Data binding a DropDownList to strings

<demo metaUrl="client/dropdownlist/data-bind/simple/" height="220"></demo>

## Bind to a Model

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.

To bind the DropDownList to a model:

1. Populate its `Data` parameter with the collection of items you want in the dropdown.
2. Set the `TextField` and `ValueField` parameters to point to the corresponding property names of the model.
3. Set the `Value` property to the initial value of the component (optional).

> The `TextField` and `ValueField` parameters must point to `string` or [value type](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types) properties. The `Value` parameter type must match the type of the `ValueField` property.

> caption Data binding a DropDownList to a model

<demo metaUrl="client/dropdownlist/data-bind/model/" height="250"></demo>

> tip If you are looking for more fields from the view-model that describes the dropdown items, not just the `Value`, see the [Get model from dropdown](https://www.telerik.com/blazor-ui/documentation/knowledge-base/dropdowns-get-model) KB article and the [OnChange](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/events#onchange) event.
>
> You may also want to review/join the discussion and Vote for this request: <a href="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model" target="_blank">Binding DropDownList Value to complex model</a>

## Considerations

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](https://www.telerik.com/blazor-ui/documentation/common-features/data-binding/overview#component-type) and what happens [if you can't provide data or a value](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#missing-value-or-data). Providing a [value that is not in the data source](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/data-bind#value-out-of-range) needs to be taken into account by the app, because the component will not change it.

### Value Out of Range

When the `Value` the application provides does not match any of the values present in the `ValueField` of the `Data` collection, the DropDownList component will not change the `Value` or select a new item. In the common case, it will show up blank to indicate there is nothing selected from its data.

If you have set the `DefaultText` and the `Value` matches the `default` value of the type (for example, `0` for an `int` or `null` for an `int?` or `string`), you will see the `DefaultText`. A `Value` that is non-`default` will not show the `DefaultText`.

Handling such "unexpected" values is up to the application - for example, through defensive checks, or through form validation, or by first checking what is present in the data source before setting a new `Value`.

### Missing Value or Data

In case you cannot provide a `Value`, `Data`, or either of them when the component initializes, you need to set the corresponding type properties to the `TItem` and `TValue` properties as shown below.

> caption DropDownList configuration if you cannot provide Value or Data

<demo metaUrl="client/dropdownlist/data-bind/missing-value-data/" height="250"></demo>

## See Also

* [Blazor DropDownList](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/overview)
* [Live Demo: DropDownList](https://demos.telerik.com/blazor-ui/dropdownlist/overview)
