You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>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 dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
3
+
>
4
+
> You may also want to review/join the discussion and Vote for this request: <ahref="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model"target="_blank">Binding DropDownList Value to complex model</a>
You can data bind the AutoComplete to a simple collection of `string` 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.
@@ -131,6 +133,7 @@ The AutoComplete component is generic and its type depends on the type of the mo
131
133
}
132
134
````
133
135
136
+
134
137
### Missing Data
135
138
136
139
The AutoComplete is, essentially, a textbox. This means that its `Value` is always a string and it is up to you to bind and/or use it. The `Data` parameter, however, is required for the functionality of the component, and it must never be `null`. If there are no suggestions that you wish to provide to the user, consider using a regular TextBox, or creating an empty collection.
Copy file name to clipboardExpand all lines: components/autocomplete/overview.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,7 @@ User input: @TheValue
29
29
Placeholder="Enter your role (can be free text)" ClearButton="true" />
30
30
31
31
@code{
32
+
//Current value is null (no item is sellected) which allows the Placeholder to be displayed.
32
33
string TheValue { get; set; }
33
34
34
35
List<string> Suggestions { get; set; } = new List<string> {
@@ -69,7 +70,7 @@ The AutoComplete is a generic component and its type is determined by the type o
69
70
70
71
*`MinLength` - how many characters the text has to be before the suggestions list appears. Cannot be `0`. Often works together with [filtering]({%slug autocomplete-filter%}).
71
72
72
-
*`Placeholder` - the text the user sees as a hint when there is no text in the input.
73
+
*`Placeholder` - the text the user sees as a hint when there is no text in the input. In order for it to be shown, the `Value` parameter should be set to the default value for string (`null`).
73
74
74
75
*`PopupHeight` - the height of the expanded dropdown list element.
75
76
@@ -87,6 +88,7 @@ The AutoComplete is a generic component and its type is determined by the type o
87
88
88
89
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
The ComboBox 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). Providing a [value that is not in the data source](#value-out-of-range) needs to be taken into account be the app, because the component will not change it.
@@ -115,7 +129,13 @@ The ComboBox is a generic component and its type comes from the model it is boun
Copy file name to clipboardExpand all lines: components/combobox/overview.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,13 @@ Selected value: @selectedValue
33
33
@code {
34
34
IEnumerable<MyDdlModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
35
35
36
-
int selectedValue { get; set; } = 3; //usually the current value should come from the model data
36
+
int selectedValue { get; set; }
37
+
38
+
//Define a preselected value when the component initializes. Placeholder will not be shown as the selected value is defined.
39
+
protected override void OnInitialized()
40
+
{
41
+
selectedValue = 3;
42
+
}
37
43
38
44
//in a real case, the model is usually in a separate file
39
45
//the model type and value field type must be provided to the dropdpownlist
@@ -75,7 +81,7 @@ The ComboBox is a generic component and its type is determined by the type of th
75
81
76
82
*`Id` - renders as the `id` attribute on the `<input />` element, so you can attach a `<label for="">` to the input.
77
83
78
-
*`Placeholder` - the text the user sees as a hint when no item is selected (the `Value`is `null`or an empty string).
84
+
*`Placeholder` - the text the user sees as a hint when no item is selected. In order for it to be shown, the `Value`parameter should be set to a default value depending on the type defined in the `ValueField` parameter. For example, `0` for an `int`, and `null`for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source.
79
85
80
86
*`PopupHeight` - the height of the expanded dropdown list element.
81
87
@@ -129,10 +135,7 @@ Missing selection is most common when the initial value is `null` as data source
129
135
| No match | No item is selected. `Value` is updated to the custom one. | No item is selected. `Value` is updated to `default(typeof(Value))`. The `OnChange` event does not fire for the value clearing. |
130
136
131
137
132
-
>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 dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
133
-
>
134
-
> You may also want to review/join the discussion and Vote for this request: <ahref="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model"target="_blank">Binding DropDownList Value to complex model</a>
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). Providing a [value that is not in the data source](#value-out-of-range) needs to be taken into account be the app, because the component will not change it.
@@ -109,9 +123,15 @@ Reference type when binding to primitive values
109
123
//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
//in a real case, the model is usually in a separate file
33
-
//the model type and value field type must be provided to the dropdpownlist
34
-
public class MyDdlModel
35
-
{
36
-
public int MyValueField { get; set; }
37
-
public string MyTextField { get; set; }
38
-
}
32
+
//in a real case, the model is usually in a separate file
33
+
//the model type and value field type must be provided to the dropdpownlist
34
+
public class MyDdlModel
35
+
{
36
+
public int MyValueField { get; set; }
37
+
public string MyTextField { get; set; }
38
+
}
39
39
40
-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
40
+
int selectedValue { get; set; }
41
41
42
-
int selectedValue { get; set; } = 3; //usually the current value should come from the model data
42
+
//Define a preselected value when the component initializes
43
+
protected override void OnInitialized()
44
+
{
45
+
selectedValue = 3;
46
+
}
47
+
48
+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
43
49
}
44
50
````
45
51
@@ -61,7 +67,7 @@ The DropDownList provides the following features:
61
67
62
68
*`Data` - allows you to provide the data source. Required.
63
69
64
-
*`DefaultText` - sets the hint that is shown if the `Value`has the `default` value for the type of the `ValueField`. For example, `0` for an `int`, and `null` for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source. You can find examples in the [Examples section](#examples) in this article and in the [Input Validation]({%slug common-features/input-validation%}#dropdownlist) article.
70
+
*`DefaultText` - simple hint to be displayed when no item is selected yet. In order for it to be shown, the `Value`parameter should be set to a default value depending on the type defined in the `ValueField` parameter. For example, `0` for an `int`, and `null` for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source. See the first example in the [Examples section](#examples) in this article and in the [Input Validation]({%slug common-features/input-validation%}#dropdownlist) article.
65
71
66
72
*`Enabled` - whether the component is enabled.
67
73
@@ -97,6 +103,15 @@ The DropDownList provides the following features:
97
103
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
98
104
99
105
106
+
## Selected Item and DefaultText
107
+
108
+
By default, if no `Value` is provided and no `DefaultText` is defined, the DropDownList will appear empty.
109
+
110
+
* To display `DefaultText` - `Value` should be `0` or `null` depending on the data type you are using in the `ValueField` and the `DefaultText` should be defined.
111
+
112
+
* To display a selected item when the component renders - provide the `Value` of the desired element. Note that it must match an item of the component's data source.
113
+
114
+
100
115
## Examples
101
116
102
117
>caption Default text (hint) to show when no actual item is selected
@@ -116,41 +131,17 @@ The DropDownList provides the following features:
>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 dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
197
-
>
198
-
> You may also want to review/join the discussion and Vote for this request: <ahref="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model"target="_blank">Binding DropDownList Value to complex model</a>
The MultiSelect 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](#reference) and what happens [if you can't provide data or a value](#missing-value-or-data).
0 commit comments