1
1
@page " /"
2
2
3
3
@using RemoteValidationWasm .Shared
4
- @using RemoteValidationWasm .Services
5
- @inject WeatherForecastService ForecastService
4
+ @using RemoteValidationWasm .Client .Services
6
5
7
- Try Inserting a record with a Date that already exists in the grid.
8
- <br />
9
- Try Editing a record and adding the letter "a" in the Summary.
10
- <br />
11
- This sample uses a Notification component to show the error message to supplement any data annotation validation in the Popup edit mode, but it can also work with other edit modes.
6
+ @inject WeatherForecastService ForecastService
12
7
13
- <TelerikGrid Data =" @forecasts" Height =" 550px" Sortable =" true" Pageable =" true" PageSize =" 20"
14
- OnUpdate =" @UpdateHandler" OnDelete =" @DeleteHandler" OnCreate =" @CreateHandler" EditMode =" @GridEditMode.Popup" >
8
+ <PageTitle >Index</PageTitle >
9
+
10
+ <p >To trigger remote validation:</p >
11
+
12
+ <ul >
13
+ <li >Add a row with a Date that already exists.</li >
14
+ <li >Update a row that does not contain <em >Q</em > in the Summary.</li >
15
+ <li >Delete one of the first five rows.</li >
16
+ </ul >
17
+
18
+ <TelerikGrid Data =" @GridData"
19
+ EditMode =" @GridEditMode.Popup"
20
+ OnCreate =" @CreateHandler"
21
+ OnUpdate =" @UpdateHandler"
22
+ OnDelete =" @DeleteHandler"
23
+ Sortable =" true"
24
+ Pageable =" true"
25
+ Height =" 500px" >
26
+ <GridToolBarTemplate >
27
+ <GridCommandButton Command =" Add" Icon =" @SvgIcon.Plus" >Add Item</GridCommandButton >
28
+ </GridToolBarTemplate >
15
29
<GridColumns >
16
- <GridColumn Field =" Id " Title = " Id " Width =" 100px" Editable =" false" Groupable =" false" />
17
- <GridColumn Field =" Date" DisplayFormat =" {0:dddd, dd MMM yyyy}" />
18
- <GridColumn Field =" TemperatureC" Title =" Temp. C" DisplayFormat =" {0:N1}" />
19
- <GridColumn Field =" TemperatureF" Title =" Temp. F" DisplayFormat =" {0:N1}" />
20
- <GridColumn Field =" Summary" />
21
- <GridCommandColumn Width =" 200px" Resizable = " false " >
22
- <GridCommandButton Command =" Save " Icon =" save " ShowInEdit = " true " >Update </GridCommandButton >
23
- <GridCommandButton Command =" Edit " Icon =" edit " ThemeColor = " @ThemeConstants.Button.ThemeColor.Primary " >Edit </GridCommandButton >
24
- <GridCommandButton Command =" Delete " Icon =" delete " >Delete </GridCommandButton >
25
- <GridCommandButton Command =" Cancel" Icon =" cancel " ShowInEdit =" true" >Cancel</GridCommandButton >
30
+ <GridColumn Field =" @nameof(WeatherForecast.Id) " Width =" 100px" Editable =" false" Groupable =" false" />
31
+ <GridColumn Field =" @nameof(WeatherForecast. Date) " DisplayFormat =" {0:dddd, dd MMM yyyy}" />
32
+ <GridColumn Field =" @nameof(WeatherForecast. TemperatureC) " Title =" Temp. C" DisplayFormat =" {0:N1}" />
33
+ <GridColumn Field =" @nameof(WeatherForecast. TemperatureF) " Title =" Temp. F" DisplayFormat =" {0:N1}" />
34
+ <GridColumn Field =" @nameof(WeatherForecast. Summary) " />
35
+ <GridCommandColumn Width =" 200px" >
36
+ <GridCommandButton Command =" Edit " Icon =" @SvgIcon.Pencil " >Edit </GridCommandButton >
37
+ <GridCommandButton Command =" Delete " Icon =" @SvgIcon.Trash " >Delete </GridCommandButton >
38
+ <GridCommandButton Command =" Save " Icon =" @SvgIcon.Save " ShowInEdit = " true " >Save </GridCommandButton >
39
+ <GridCommandButton Command =" Cancel" Icon =" @SvgIcon.Cancel " ShowInEdit =" true" >Cancel</GridCommandButton >
26
40
</GridCommandColumn >
27
41
</GridColumns >
28
- <GridToolBar >
29
- <GridCommandButton Command =" Add" Icon =" plus" ThemeColor =" @ThemeConstants.Button.ThemeColor.Primary" >Add Forecast</GridCommandButton >
30
- </GridToolBar >
31
42
</TelerikGrid >
32
43
33
- <TelerikNotification @ref =" @Notification "
44
+ <TelerikNotification @ref =" @NotificationRef "
34
45
HorizontalPosition =" @NotificationHorizontalPosition.Center"
35
46
VerticalPosition =" @NotificationVerticalPosition.Top"
36
- Class =" big-notification " >
47
+ Class =" above-modal-overlay " >
37
48
</TelerikNotification >
38
49
50
+ <style >
51
+ .above-modal-overlay {
52
+ z-index : 22222 ;
53
+ }
54
+ </style >
39
55
40
56
@code {
41
- List <WeatherForecast > forecasts { get ; set ; }
42
- bool errorExists { get ; set ; }
43
- TelerikNotification Notification { get ; set ; }
57
+ private List <WeatherForecast > GridData { get ; set ; } = new ();
58
+
59
+ #region UI for Validation Errors
60
+
61
+ private TelerikNotification ? NotificationRef { get ; set ; }
44
62
45
- void ShowErrorNotification (string message )
63
+ private void ShowErrorNotification (string message )
46
64
{
47
- Notification .Show (new NotificationModel
65
+ NotificationRef ? .Show (new NotificationModel
48
66
{
49
67
CloseAfter = 0 ,
50
68
Text = message ,
51
69
ThemeColor = ThemeConstants .Notification .ThemeColor .Error
52
70
});
53
71
}
54
72
55
- protected override async Task OnInitializedAsync ()
56
- {
57
- await GetForecasts ();
58
- }
73
+ #endregion UI for Validation Errors
59
74
60
- async Task GetForecasts ()
61
- {
62
- forecasts = await ForecastService .GetForecastListAsync (DateTime .Now );
63
- }
64
-
65
- // sample error handling for the CUD operations - in this sample through simplistic error messsages coming from the server
75
+ #region Grid Data Operations
66
76
67
- public async Task DeleteHandler (GridCommandEventArgs args )
77
+ private async Task CreateHandler (GridCommandEventArgs args )
68
78
{
69
- WeatherForecast currItem = args .Item as WeatherForecast ;
79
+ WeatherForecast currItem = ( WeatherForecast ) args .Item ;
70
80
71
81
try
72
82
{
73
- await ForecastService .DeleteForecastAsync (currItem );
74
- // update the view data
75
- forecasts . Remove ( currItem );
83
+ WeatherForecast insertedForecast = await ForecastService .InsertForecastAsync (currItem );
84
+ // Update the view data, otherwise reload the Grid data.
85
+ GridData . Insert ( 0 , insertedForecast );
76
86
}
77
87
catch (Exception ex )
78
88
{
79
- // show information to the user
89
+ // Keep the Grid in edit mode.
90
+ args .IsCancelled = true ;
91
+
80
92
ShowErrorNotification (ex .Message );
81
93
}
82
94
}
83
95
84
- public async Task CreateHandler (GridCommandEventArgs args )
96
+ private async Task UpdateHandler (GridCommandEventArgs args )
85
97
{
86
- WeatherForecast currItem = args .Item as WeatherForecast ;
98
+ WeatherForecast currItem = (WeatherForecast )args .Item ;
99
+
100
+ await Task .Delay (1 );
87
101
88
102
try
89
103
{
90
- // get the inserted data from the server in case it alters it further (e.g., provides an ID)
91
- WeatherForecast insertedForecast = await ForecastService .InsertForecastAsync (currItem );
92
- // update the view data
93
- forecasts .Insert (0 , insertedForecast );
104
+ WeatherForecast updatedForecast = await ForecastService .UpdateForecastAsync (currItem );
105
+ // Update the view data, otherwise reload the Grid data.
106
+ var index = GridData .FindIndex (i => i .Id == updatedForecast .Id );
107
+ if (index != - 1 )
108
+ {
109
+ GridData [index ] = updatedForecast ;
110
+ }
94
111
}
95
112
catch (Exception ex )
96
113
{
97
- // keep the grid in insert mode
114
+ // Keep the Grid in edit mode.
98
115
args .IsCancelled = true ;
99
- // show information to the user
116
+
100
117
ShowErrorNotification (ex .Message );
101
118
}
102
119
}
103
120
104
- public async Task UpdateHandler (GridCommandEventArgs args )
121
+ private async Task DeleteHandler (GridCommandEventArgs args )
105
122
{
106
- WeatherForecast currItem = args .Item as WeatherForecast ;
123
+ WeatherForecast currItem = ( WeatherForecast ) args .Item ;
107
124
108
125
try
109
126
{
110
- // get the inserted data from the server in case it alters it further
111
- WeatherForecast updatedForecast = await ForecastService .UpdateForecastAsync (currItem );
112
- // update the view data
113
- var index = forecasts .FindIndex (i => i .Id == updatedForecast .Id );
114
- if (index != - 1 )
115
- {
116
- forecasts [index ] = updatedForecast ;
117
- }
127
+ await ForecastService .DeleteForecastAsync (currItem );
128
+ // Update the view data, otherwise reload the Grid data.
129
+ GridData .Remove (currItem );
118
130
}
119
131
catch (Exception ex )
120
132
{
121
- // keep the grid in edit mode
122
- args .IsCancelled = true ;
123
- // show information to the user
124
133
ShowErrorNotification (ex .Message );
125
134
}
126
135
}
127
- }
128
136
129
- <style >
130
- .big-notification .k-notification-container .k-notification-wrap {
131
- width : 350px ;
132
- height : 100px ;
133
- align-items : center ;
134
- font-size : 32px ;
135
- }
137
+ #endregion Grid Data Operations
136
138
137
- .big-notification .k-notification-container .k-notification-wrap .k-icon.k-i-error ::before {
138
- font-size : 32px ;
139
- }
139
+ #region Grid Data Generation
140
+
141
+ protected override async Task OnInitializedAsync ()
142
+ {
143
+ await GetForecasts ();
144
+ }
140
145
141
- .big-notification {
142
- z-index : 654321 ;
146
+ private async Task GetForecasts ()
147
+ {
148
+ GridData = await ForecastService .GetForecastListAsync (DateTime .Now );
143
149
}
144
- </style >
150
+
151
+ #endregion Grid Data Generation
152
+ }
0 commit comments