@@ -19,16 +19,16 @@ To use a Telerik DropDownList for Blazor
19
19
1 . set the ` TextField ` and ` ValueField ` properties to point to the corresponding names of the model
20
20
1 . set the ` Value ` property to the intial value of the model.
21
21
22
- > caption Basic dropdownlist [ data binding] ( data-bind ) and ValueChanged event handling
23
-
24
- @[ template] ( /_contentTemplates/common/issues-and-warnings.md#generic-component-event-issue )
22
+ > caption Basic dropdownlist [ data binding] ( data-bind ) and value binding
25
23
26
24
```` CSHTML
27
25
@using Telerik.Blazor.Components.DropDownList
28
26
29
- <TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" ValueChanged="@MyValueChangedHandler" @bind-Value="selectedValue">
27
+ <TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="selectedValue">
30
28
</TelerikDropDownList>
31
29
30
+ <br />Selected value: @selectedValue
31
+
32
32
@code {
33
33
//in a real case, the model is usually in a separate file
34
34
//the model type and value field type must be provided to the dropdpownlist
@@ -41,14 +41,7 @@ To use a Telerik DropDownList for Blazor
41
41
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
42
42
43
43
int selectedValue { get; set; } = 3; //usually the current value should come from the model data
44
-
45
- void MyValueChangedHandler(object newValue)
46
- {
47
- //the data item (model) that the user selected
48
- Console.WriteLine((newValue as MyDdlModel).MyTextField);
49
- }
50
44
}
51
-
52
45
````
53
46
54
47
> caption The result from the code snippet above
@@ -74,6 +67,18 @@ To use a Telerik DropDownList for Blazor
74
67
public string MyTextField { get; set; }
75
68
}
76
69
}
70
+
71
+ <TelerikDropDownList @ref="myDdlRef2" Data="@MyList" @bind-Value="MyItem">
72
+ </TelerikDropDownList>
73
+
74
+ @code {
75
+ protected List<string> MyList = new List<string>() { "first", "second", "third" };
76
+
77
+ protected string MyItem { get; set; } = "second";
78
+
79
+ //the type of the generic component is determined by the type of the model you pass to it, when the model is a primitive type
80
+ Telerik.Blazor.Components.DropDownList.TelerikDropDownList<string, string> myDdlRef2;
81
+ }
77
82
````
78
83
79
84
The DropDownList provides the following features:
@@ -97,70 +102,56 @@ The DropDownList provides the following features:
97
102
* ` string `
98
103
* ` Guid `
99
104
* ` Enum `
100
-
101
105
* ` Width ` - the width of the dropdown and the main element.
102
106
* Templates - they allow you to control the rendering of items in the component. See the [ Templates] ({%slug components/dropdownlist/templates%}) article for more details.
103
107
* Validation - see the [ Input Validation] ({%slug common-features/input-validation%}) article for more details.
104
108
105
109
106
110
## Examples
107
111
108
-
109
- > caption Validating a dropdownlist. See the comments in the code for details on the behavior
112
+ > caption Handling the ValueChanged event and providing an initial value
110
113
111
114
```` CSHTML
112
115
@using Telerik.Blazor.Components.DropDownList
113
- @using System.ComponentModel.DataAnnotations
114
116
115
- <EditForm Model="@person" OnValidSubmit="@HandleValidSubmit">
116
- <DataAnnotationsValidator />
117
- <ValidationSummary />
118
- <p class="gender">
119
- Gender: <TelerikDropDownList @bind-Value="person.Gender" DefaultItem="@ddlHint"
120
- Data="@genders" TextField="MyTextField" ValueField="MyValueField">
121
- </TelerikDropDownList>
122
- <ValidationMessage For="@(() => person.Gender)"></ValidationMessage>
123
- </p>
117
+ <TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField"
118
+ Value="@InitialValue" ValueChanged="@( (int v) => MyValueChangedHandler(v) )">
119
+ </TelerikDropDownList>
124
120
125
- <button type="submit">Submit</button>
126
- </EditForm>
121
+ <br />
122
+ @result
123
+ <br />
124
+ @InitialValue
127
125
128
126
@code {
129
- // Usually the model classes would be in different files
130
- public class Person
131
- {
132
- [Required(ErrorMessage = "Gender is mandatory.")]//the value field in the dropdown model must be null in the default item
133
- [Range(1, 3, ErrorMessage = "Please select your gender.")] //limits the fourth option just to showcase this is honored
134
- public int? Gender { get; set; }
135
- }
127
+ IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
136
128
137
- public class MyDdlModel
138
- {
139
- //nullable so the default item can allow required field validation
140
- //alternatively, use a range validator and put a value out of that range for the default item
141
- public int? MyValueField { get; set; }
142
- public string MyTextField { get; set; }
143
- }
129
+ int InitialValue { get; set; } = 3; // an intial value is not required, this example showcases how to set it
144
130
145
- Person person = new Person();
131
+ string result { get; set; }
146
132
147
- MyDdlModel ddlHint = new MyDdlModel { MyValueField = null, MyTextField = "Gender" };
133
+ public class MyDdlModel
134
+ {
135
+ public int MyValueField { get; set; }
136
+ public string MyTextField { get; set; }
137
+ }
148
138
149
- IEnumerable<MyDdlModel> genders = new List<MyDdlModel>
150
- {
151
- new MyDdlModel {MyTextField = "female", MyValueField = 1},
152
- new MyDdlModel {MyTextField = "male", MyValueField = 2},
153
- new MyDdlModel {MyTextField = "other", MyValueField = 3},
154
- new MyDdlModel {MyTextField = "I'd rather not say", MyValueField = 4}
155
- };
139
+ async Task MyValueChangedHandler(int newVal)
140
+ {
141
+ // the type of the value field in the model determines the signature of the handler
142
+ result = $"The user selected {newVal}";
156
143
157
- void HandleValidSubmit()
158
- {
159
- Console.WriteLine("OnValidSubmit") ;
160
- }
144
+ // handling ValueChanged does not let you use value binding, so if you need to update the model
145
+ // you must do that manually in the handler. This is not required, though
146
+ InitialValue = newVal ;
147
+ }
161
148
}
162
149
````
163
150
151
+ @[ template] ( /_contentTemplates/common/general-info.md#event-callback-can-be-async )
152
+
153
+ @[ template] ( /_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required )
154
+
164
155
165
156
> caption Get selected item from external code
166
157
0 commit comments