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
This article explains the different ways to provide data to an AutoComplete component, the properties related to data binding and their results. The key requirements is to have a string field for the suggestions.
13
+
This article explains the different ways to provide data to a MultiSelect component, the properties related to data binding and their results.
14
14
15
15
There are two key ways to bind data:
16
16
17
-
*[Primitive Type](#primitive-type)
17
+
*[Primitive Types](#primitive-types)
18
18
*[Model](#bind-to-a-model)
19
19
20
20
There are also some [considerations](#considerations) to keep in mind.
21
21
22
22
## Primitive Types
23
23
24
-
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.
24
+
You can data bind the MultiSelect to a simple collection of data (number - such as `int`, `double` and so on, `string`, `Guid`, `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.
25
25
26
-
To bind the AutoComplete, you need to:
26
+
To bind the MultiSelect, you need to:
27
27
28
28
1. provide an `IEnumerable<string>` to its `Data` property,
29
-
1. point the `Value` parameter to a `string` field.
29
+
1. point the `Value` parameter to a `List<string>` field.
30
30
31
-
>caption Data binding an AutoComplete to a simple data source
31
+
>caption Data binding an MultiSelect to a simple string data source
You can bind the AutoComplete to a model in your application. This is useful when you have the data in some form already and you don't need to prepare a separate collection of suggestions.
82
+
You can bind the MultiSelect to a model in your application. This is useful when you have the data in some form already and you don't need to prepare a separate collection of suggestions.
57
83
58
-
To bind the AutoComplete to a model:
84
+
To bind the MultiSelect to a model:
59
85
60
86
1. populate its `Data` property with the collection of items you want in the dropdown
61
-
1. set the `ValueField` to point to the corresponding name of the model that contains the string data for the suggestions
62
-
1. point the `Value` parameter to a `string` field in the view-model.
87
+
1. set the `ValueField`and the `TextField`to point to the corresponding fields of the model that contain, sespectively, the value of the options and the string data for them.
88
+
1. point the `Value` parameter to a `List<TValue>` field in the view-model.
List<SuggestionsModel> Suggestions { get; set; } = new List<SuggestionsModel>
109
+
List<int> TheValues { get; set; }
110
+
List<OptionsModel> Options { get; set; } = new List<OptionsModel>
75
111
{
76
-
new SuggestionsModel { Suggestion = "first", SomeOtherField = 1 },
77
-
new SuggestionsModel { Suggestion = "second", SomeOtherField = 2 },
78
-
new SuggestionsModel { Suggestion = "third", SomeOtherField = 3 }
112
+
new OptionsModel { StringRepresentation = "first", MyValueField = 1 },
113
+
new OptionsModel { StringRepresentation = "second", MyValueField = 2 },
114
+
new OptionsModel { StringRepresentation = "third", MyValueField = 3 }
79
115
};
80
116
81
-
public class SuggestionsModel
117
+
public class OptionsModel
82
118
{
83
-
public string Suggestion { get; set; }//the auto complete needs only the string field
84
-
public int SomeOtherField { get; set; }
119
+
public string StringRepresentation { get; set; }
120
+
public int MyValueField { get; set; } // this determines the type of the values list
85
121
}
86
122
}
87
123
````
88
124
89
125
## Considerations
90
126
127
+
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).
128
+
91
129
### Reference
92
130
93
-
The AutoComplete component is generic and its type depends on the type of the model you provide as its `Data` collection.
131
+
The MultiSelect component is generic and its type depends on the type of the model you provide as its `Data` collection.
94
132
95
133
````Primitive
96
134
@*Reference type when binding to primitive collections*@
List<SuggestionsModel> Suggestions { get; set; } = new List<SuggestionsModel>
155
+
List<int> TheValues { get; set; }
156
+
List<OptionsModel> Options { get; set; } = new List<OptionsModel>
118
157
{
119
-
new SuggestionsModel { Suggestion = "first", SomeOtherField = 1 },
120
-
new SuggestionsModel { Suggestion = "second", SomeOtherField = 2 },
121
-
new SuggestionsModel { Suggestion = "third", SomeOtherField = 3 }
158
+
new OptionsModel { StringRepresentation = "first", MyValueField = 1 },
159
+
new OptionsModel { StringRepresentation = "second", MyValueField = 2 },
160
+
new OptionsModel { StringRepresentation = "third", MyValueField = 3 }
122
161
};
123
162
124
-
public class SuggestionsModel
163
+
public class OptionsModel
125
164
{
126
-
public string Suggestion { get; set; }//the auto complete needs only the string field
127
-
public int SomeOtherField { get; set; }
165
+
public string StringRepresentation { get; set; }
166
+
public int MyValueField { get; set; } // this determines the type of the values list
128
167
}
129
168
}
130
169
````
131
170
132
-
### Missing Data
171
+
### Missing Value Or Data
133
172
134
-
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.
173
+
In case you cannot provide either of a `Value`, or `Data`, or both when the component initializes, you need to set the corresponding type properties to the `TItem` and `TValue` properties as shown below.
135
174
136
-
>caption Minimal AutoComplete configuration for it to run
175
+
>caption MultiSelect configuration if you cannot provide Value or Data
137
176
138
177
````CSHTML
139
-
@* If you cannot provide suggestions list, use an empty collection and the component will show "No Data" to the user in the suggestions list *@
178
+
@*How to declare the multiselect if no Value or Data are provided*@
0 commit comments