Skip to content

Commit c75403a

Browse files
kb(dropdowns): get selected model
1 parent 362a032 commit c75403a

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Get model from dropodwn
3+
description: how to get a model from a dropdown instead of a primitive value
4+
type: how-to
5+
page_title: Get model from dropdown
6+
slug: dropdowns-get-model
7+
position:
8+
tags:
9+
ticketid: 1452556
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>MultiSelect for Blazor, DropDownList for Blazor, ComboBox for Blazor, AutoCopmlete for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
26+
I want to get an instance of my model when I select an item from a dropdown (such as a DropDownList, ComboBox, AutoComplete, MultiSelect). I can get only a primitive type that is the type of the `Value` and `ValueField`.
27+
28+
The dropdowns provide a primitive `Value` so that [validation]({%slug common-features/input-validation%}) can work, and so that other data source operations (such as filtering) can work. The Value and Text cannot be classes (models) because that would prevent validation from working and filtering/comparing entire classes is an operation that is not defined.
29+
30+
## Solution
31+
32+
The solution is to use the unique identifier you get from the component (the `Value`) and to get the entire model from its data source (the `Data` collection) by filtering it (e.g., by using the `Where()` operator).
33+
34+
The example below uses the DropDownList component, and the same approach is applicable for the others as well. For the MultiSelect you will have to loop over the selected values collection, of course, and for the AutoComplete you may want to ensure unique Text values (the autocomplete is a free text input with suggestions, not a dropdown with mandatory choices).
35+
36+
>caption Get model from dropdown
37+
38+
````CSHTML
39+
@result
40+
<br />
41+
42+
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField"
43+
Value="@DdlValue" ValueChanged="@( (int v) => ValueChangedHandler(v) )" DefaultText="Select something">
44+
</TelerikDropDownList>
45+
46+
<br />
47+
48+
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField"
49+
@bind-Value="@DdlValue" DefaultText="Select something">
50+
</TelerikDropDownList>
51+
52+
<TelerikButton OnClick="@GetSelectedItem">Get Selected Item</TelerikButton>
53+
54+
@code {
55+
string result;
56+
int DdlValue { get; set; } = 5;
57+
58+
void GetSelectedItem()
59+
{
60+
GetItemFromModelData();
61+
}
62+
63+
void ValueChangedHandler(int v)
64+
{
65+
DdlValue = v;
66+
67+
GetItemFromModelData();
68+
}
69+
70+
void GetItemFromModelData()
71+
{
72+
// extract the data item from the data source by using the value
73+
MyDdlModel selectedItem = myDdlData.Where(d => d.MyValueField == DdlValue).FirstOrDefault();
74+
if (selectedItem != null) // e.g., custom text in a combo, or no match for an autocomplete
75+
{
76+
result = selectedItem.MyTextField;
77+
}
78+
else
79+
{
80+
result = "no item selected";
81+
}
82+
83+
StateHasChanged();
84+
}
85+
86+
public class MyDdlModel
87+
{
88+
public int MyValueField { get; set; }
89+
public string MyTextField { get; set; }
90+
}
91+
92+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
93+
}
94+
````
95+

0 commit comments

Comments
 (0)