@@ -89,59 +89,11 @@ For development environments, we use `EnsureCreatedAsync()` to automatically cre
89
89
90
90
Now we'll update the web application to support favoriting weather zones and filtering them. Let's make these changes step by step:
91
91
92
- 1 . First, add a checkbox to filter the zones list. In ` Home.razor ` , add this code just before the ` <QuickGrid> ` element:
93
-
94
- ``` csharp
95
- < div class="form-check mb-3">
96
- <input class="form-check-input" type="checkbox" @bind="ShowOnlyFavorites" id="showFavorites">
97
- <label class="form-check-label" for="showFavorites">
98
- Show only favorites
99
- </label>
100
- </div>
101
- ```
102
-
103
- 1. Next, add a new column to show the favorite status. Add this column definition inside the `<QuickGrid>` element, after the existing State column:
92
+ 1 . Make sure to add these Entity Framework using statements at the top of ` Home.razor ` if they're not already present:
104
93
105
94
``` csharp
106
- <TemplateColumn Title="Favorite">
107
- <ChildContent>
108
- <button @onclick="@(() => ToggleFavorite(context))">
109
- @if (FavoriteZones.Contains(context))
110
- {
111
- <span>★</span> <!-- Starred -->
112
- }
113
- else
114
- {
115
- < span > & #9734 ;< / span > < ! -- Unstarred -- >
116
- }
117
- < / button >
118
- < / ChildContent >
119
- < / TemplateColumn >
120
- ```
121
-
122
- 1 . In the ` @code ` block of ` Home.razor ` , locate the ` zones ` property and replace it with this updated version that includes the favorites filter:
123
-
124
- ``` csharp
125
- IQueryable < Zone > zones
126
- {
127
- get
128
- {
129
- var results = AllZones .AsQueryable ();
130
-
131
- if (ShowOnlyFavorites )
132
- {
133
- results = results .Where (z => FavoriteZones .Contains (z ));
134
- }
135
-
136
- results = string .IsNullOrEmpty (StateFilter ) ? results
137
- : results .Where (z => z .State == StateFilter .ToUpper ());
138
-
139
- results = string .IsNullOrEmpty (NameFilter ) ? results
140
- : results .Where (z => z .Name .Contains (NameFilter , StringComparison .InvariantCultureIgnoreCase ));
141
-
142
- return results .OrderBy (z => z .Name );
143
- }
144
- }
95
+ @using Microsoft .EntityFrameworkCore
96
+ @inject MyWeatherContext DbContext
145
97
```
146
98
147
99
1 . Add these new properties to the ` @code ` block to support the favorites functionality:
@@ -157,7 +109,7 @@ List<Zone> FavoriteZones { get; set; } = new List<Zone>();
157
109
protected override async Task OnInitializedAsync ()
158
110
{
159
111
AllZones = (await NwsManager .GetZonesAsync ()).ToArray ();
160
- FavoriteZones = await DbContext .FavoriteZones .ToListAsync ();
112
+ FavoriteZones = await MyWeatherContext .FavoriteZones .ToListAsync ();
161
113
}
162
114
```
163
115
@@ -169,22 +121,70 @@ private async Task ToggleFavorite(Zone zone)
169
121
if (FavoriteZones .Contains (zone ))
170
122
{
171
123
FavoriteZones .Remove (zone );
172
- DbContext .FavoriteZones .Remove (zone );
124
+ MyWeatherContext .FavoriteZones .Remove (zone );
173
125
}
174
126
else
175
127
{
176
128
FavoriteZones .Add (zone );
177
- DbContext .FavoriteZones .Add (zone );
129
+ MyWeatherContext .FavoriteZones .Add (zone );
178
130
}
179
131
await DbContext .SaveChangesAsync ();
180
132
}
181
133
```
182
134
183
- Make sure to add these Entity Framework using statements at the top of ` Home.razor ` if they're not already present :
135
+ 1 . In the ` @code ` block of ` Home.razor ` , locate the ` zones ` property and replace it with this updated version that includes the favorites filter :
184
136
185
137
``` csharp
186
- @using Microsoft .EntityFrameworkCore
187
- @inject MyWeatherContext DbContext
138
+ IQueryable < Zone > zones
139
+ {
140
+ get
141
+ {
142
+ var results = AllZones .AsQueryable ();
143
+
144
+ if (ShowOnlyFavorites )
145
+ {
146
+ results = results .Where (z => FavoriteZones .Contains (z ));
147
+ }
148
+
149
+ results = string .IsNullOrEmpty (StateFilter ) ? results
150
+ : results .Where (z => z .State == StateFilter .ToUpper ());
151
+
152
+ results = string .IsNullOrEmpty (NameFilter ) ? results
153
+ : results .Where (z => z .Name .Contains (NameFilter , StringComparison .InvariantCultureIgnoreCase ));
154
+
155
+ return results .OrderBy (z => z .Name );
156
+ }
157
+ }
158
+ ```
159
+
160
+ 1 . First, add a checkbox to filter the zones list. In ` Home.razor ` , add this code just before the ` <QuickGrid> ` element:
161
+
162
+ ``` csharp
163
+ < div class="form-check mb-3">
164
+ <input class="form-check-input" type="checkbox" @bind="ShowOnlyFavorites" id="showFavorites">
165
+ <label class="form-check-label" for="showFavorites">
166
+ Show only favorites
167
+ </label>
168
+ </div>
169
+ ```
170
+
171
+ 1. Next, add a new column to show the favorite status. Add this column definition inside the `<QuickGrid>` element, after the existing State column:
172
+
173
+ ```csharp
174
+ <TemplateColumn Title="Favorite">
175
+ <ChildContent>
176
+ <button @onclick="@(() => ToggleFavorite(context))">
177
+ @if (FavoriteZones.Contains(context))
178
+ {
179
+ <span>★</span> <!-- Starred -->
180
+ }
181
+ else
182
+ {
183
+ < span > & #9734 ;< / span > < ! -- Unstarred -- >
184
+ }
185
+ < / button >
186
+ < / ChildContent >
187
+ < / TemplateColumn >
188
188
```
189
189
190
190
## Testing Your Changes
0 commit comments