Skip to content

Commit a2def96

Browse files
docs(dropdownlist): value changed example and improved overview snippets
1 parent 7d0eff5 commit a2def96

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#event-callback-can-be-async
2+
>tip The event is an `EventCallback` and its type can be `void`, or it can also be asynchronous and return `async Task`.
3+
#end

components/dropdownlist/overview.md

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ To use a Telerik DropDownList for Blazor
1919
1. set the `TextField` and `ValueField` properties to point to the corresponding names of the model
2020
1. set the `Value` property to the intial value of the model.
2121

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
2523
2624
````CSHTML
2725
@using Telerik.Blazor.Components.DropDownList
2826
29-
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" ValueChanged="@MyValueChangedHandler" @bind-Value="selectedValue">
27+
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="selectedValue">
3028
</TelerikDropDownList>
3129
30+
<br />Selected value: @selectedValue
31+
3232
@code {
3333
//in a real case, the model is usually in a separate file
3434
//the model type and value field type must be provided to the dropdpownlist
@@ -41,14 +41,7 @@ To use a Telerik DropDownList for Blazor
4141
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
4242
4343
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-
}
5044
}
51-
5245
````
5346

5447
>caption The result from the code snippet above
@@ -74,6 +67,18 @@ To use a Telerik DropDownList for Blazor
7467
public string MyTextField { get; set; }
7568
}
7669
}
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+
}
7782
````
7883

7984
The DropDownList provides the following features:
@@ -97,70 +102,56 @@ The DropDownList provides the following features:
97102
* `string`
98103
* `Guid`
99104
* `Enum`
100-
101105
* `Width` - the width of the dropdown and the main element.
102106
* Templates - they allow you to control the rendering of items in the component. See the [Templates]({%slug components/dropdownlist/templates%}) article for more details.
103107
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
104108

105109

106110
## Examples
107111

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
110113
111114
````CSHTML
112115
@using Telerik.Blazor.Components.DropDownList
113-
@using System.ComponentModel.DataAnnotations
114116
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>
124120
125-
<button type="submit">Submit</button>
126-
</EditForm>
121+
<br />
122+
@result
123+
<br />
124+
@InitialValue
127125
128126
@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 });
136128
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
144130
145-
Person person = new Person();
131+
string result { get; set; }
146132
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+
}
148138
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}";
156143
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+
}
161148
}
162149
````
163150

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+
164155

165156
>caption Get selected item from external code
166157

0 commit comments

Comments
 (0)