@@ -140,6 +140,155 @@ Implementing beautiful rendering is up to the application, this example shows th
140
140
141
141
![ editing in the listview] ( images/listview-editing-sample.png )
142
142
143
+ > tip You can add validation in the edit/insert templates as well, and handle it by cancelling the ` OnUpdate ` and ` OnCreate ` events depending on the result of the validation (be that local ` DataAnnotation ` validation, or remote validation through your data service).
144
+
145
+ > caption Validation in ListView editing
146
+
147
+ ```` CSHTML
148
+ @using System.ComponentModel.DataAnnotations
149
+
150
+ <TelerikListView Data="@ListViewData" Pageable="true"
151
+ OnCreate="@CreateHandler" OnDelete="@DeleteHandler" OnUpdate="@UpdateHandler" OnCancel="@CancelHandler">
152
+ <EditTemplate>
153
+ <div style="border: 1px solid green; margin: 10px; padding: 10px; display: inline-block;">
154
+ @{
155
+ currEditItem = context;
156
+ if(currEditContext == null)
157
+ {
158
+ currEditContext = new EditContext(currEditItem);
159
+ }
160
+ <EditForm EditContext="@currEditContext" Context="formContext">
161
+ <DataAnnotationsValidator />
162
+ <TelerikTextBox @bind-Value="@currEditItem.Name" Label="Name" />
163
+ <ValidationMessage For="@(() => currEditItem.Name)"></ValidationMessage>
164
+
165
+ <br />
166
+ <TelerikDropDownList Data="@Teams" @bind-Value="@currEditItem.Team" />
167
+
168
+ <ListViewCommandButton Command="Save" Icon="@IconName.Save">Save</ListViewCommandButton>
169
+ <ListViewCommandButton Command="Cancel" Icon="@IconName.Cancel">Cancel</ListViewCommandButton>
170
+ </EditForm>
171
+ }
172
+ </div>
173
+ </EditTemplate>
174
+ <Template>
175
+ <div style="border: 1px solid black; margin: 10px; padding: 10px; display: inline-block;">
176
+ Employee: @context.Id <br />
177
+ Name: @context.Name in team: @context.Team
178
+ <ListViewCommandButton Command="Edit" Icon="@IconName.Edit">Edit</ListViewCommandButton>
179
+ <ListViewCommandButton Command="Delete" Icon="@IconName.Delete">Delete</ListViewCommandButton>
180
+ </div>
181
+ </Template>
182
+ <HeaderTemplate>
183
+ <ListViewCommandButton Command="Add" Icon="@IconName.Plus">Add Employee</ListViewCommandButton>
184
+ <p>In this sample, the first item will not open for editing because of the code in the OnEdit handler</p>
185
+ </HeaderTemplate>
186
+ </TelerikListView>
187
+
188
+ @code{
189
+ Employee currEditItem { get; set; }
190
+ EditContext currEditContext { get; set; }
191
+
192
+ async Task CreateHandler(ListViewCommandEventArgs e)
193
+ {
194
+ Employee insertedItem = e.Item as Employee;
195
+
196
+ // sample validation
197
+ if (!currEditContext.Validate())
198
+ {
199
+ // prevent the listview from going back in view mode
200
+ e.IsCancelled = true;
201
+ return;
202
+ }
203
+
204
+ // if you need remote validation (e.g., check for duplicates on the server)
205
+ // add it here through your data service and cancel the event as well
206
+
207
+ // save actual data here through your service
208
+
209
+ // update the view-model, can use returned data from the remote service too
210
+ insertedItem.Id = ListViewData.Count + 1;
211
+ ListViewData.Insert(0, insertedItem);
212
+
213
+ CleanUpValidation();
214
+ }
215
+
216
+ async Task DeleteHandler(ListViewCommandEventArgs e)
217
+ {
218
+ Employee deletedItem = e.Item as Employee;
219
+
220
+ ListViewData.Remove(deletedItem);
221
+
222
+ // save to the actual data source here as well
223
+ }
224
+
225
+ async Task UpdateHandler(ListViewCommandEventArgs e)
226
+ {
227
+ Employee updatedItem = e.Item as Employee;
228
+
229
+ // sample validation
230
+ if (!currEditContext.Validate())
231
+ {
232
+ // prevent the listview from going back in view mode
233
+ e.IsCancelled = true;
234
+ return;
235
+ }
236
+
237
+ // if you need remote validation (e.g., check for duplicates on the server)
238
+ // add it here through your data service and cancel the event as well
239
+
240
+ // save actual data here through your service
241
+
242
+ // update the view-model, can use returned data from the remote service too
243
+ int index = ListViewData.FindIndex(itm => itm.Id == updatedItem.Id);
244
+ if (index > -1)
245
+ {
246
+ ListViewData[index] = updatedItem;
247
+ }
248
+
249
+ CleanUpValidation();
250
+ }
251
+
252
+ void CancelHandler(ListViewCommandEventArgs e)
253
+ {
254
+ CleanUpValidation();
255
+ }
256
+
257
+ void CleanUpValidation()
258
+ {
259
+ // clean up for next run
260
+ currEditContext = null;
261
+ currEditItem = null;
262
+ }
263
+
264
+ // data and models follow
265
+
266
+ List<Employee> ListViewData { get; set; }
267
+
268
+ protected override void OnInitialized()
269
+ {
270
+ ListViewData = Enumerable.Range(1, 250).Select(x => new Employee
271
+ {
272
+ Id = x,
273
+ Name = $"Name {x}",
274
+ Team = Teams[x % Teams.Count]
275
+ }).ToList();
276
+ }
277
+
278
+ List<string> Teams = new List<string> { "Sales", "Dev", "Support" };
279
+
280
+ public class Employee
281
+ {
282
+ public int Id { get; set; }
283
+
284
+ [Required(ErrorMessage = "Enter a name")]
285
+ public string Name { get; set; }
286
+
287
+ public string Team { get; set; }
288
+ }
289
+ }
290
+ ````
291
+
143
292
## See Also
144
293
145
294
* [ ListView Overview] ({%slug listview-overview%})
0 commit comments