@@ -158,89 +158,122 @@ public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dict
158
158
return new HtmlString ( sb . ToString ( ) ) ;
159
159
}
160
160
161
- public static void SetGriddlyDefault < T > ( this Controller controller , ref T parameter , string field , T value )
161
+ static readonly string _contextKey = "_griddlycontext" ;
162
+
163
+ public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T parameter , string field , T value )
162
164
{
163
- if ( controller . ControllerContext . IsChildAction )
165
+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
166
+
167
+ if ( context != null )
164
168
{
165
- if ( EqualityComparer < T > . Default . Equals ( parameter , default ( T ) ) )
169
+ context . Defaults [ field ] = value ;
170
+
171
+ if ( controller . ControllerContext . IsChildAction
172
+ && ! context . IsDefaultSkipped
173
+ && EqualityComparer < T > . Default . Equals ( parameter , default ( T ) ) )
174
+ {
166
175
parameter = value ;
167
176
168
- controller . ViewData [ "_griddlyDefault_" + field ] = parameter ;
177
+ context . Parameters [ field ] = value ;
178
+ }
169
179
}
170
- else
171
- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
172
180
}
173
181
174
- public static void SetGriddlyDefault < T > ( this Controller controller , ref T [ ] parameter , string field , IEnumerable < T > value )
182
+ public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T [ ] parameter , string field , IEnumerable < T > value )
175
183
{
176
- if ( controller . ControllerContext . IsChildAction )
184
+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
185
+
186
+ if ( context != null )
177
187
{
178
- if ( parameter == null )
179
- parameter = value . ToArray ( ) ;
188
+ context . Defaults [ field ] = value ;
180
189
181
- controller . ViewData [ "_griddlyDefault_" + field ] = parameter ;
190
+ if ( controller . ControllerContext . IsChildAction
191
+ && ! context . IsDefaultSkipped
192
+ && parameter == null )
193
+ {
194
+ parameter = value . ToArray ( ) ;
195
+ }
182
196
}
183
- else
184
- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
185
197
}
186
198
187
- public static void SetGriddlyDefault < T > ( this Controller controller , ref T ? [ ] parameter , string field , IEnumerable < T > value )
199
+ public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T ? [ ] parameter , string field , IEnumerable < T > value )
188
200
where T : struct
189
201
{
190
- if ( controller . ControllerContext . IsChildAction )
202
+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
203
+
204
+ if ( context != null )
191
205
{
192
- if ( parameter == null )
193
- parameter = value . Cast < T ? > ( ) . ToArray ( ) ;
206
+ context . Defaults [ field ] = value ;
194
207
195
- controller . ViewData [ "_griddlyDefault_" + field ] = parameter ;
208
+ if ( controller . ControllerContext . IsChildAction
209
+ && ! context . IsDefaultSkipped
210
+ && parameter == null )
211
+ {
212
+ parameter = value . Cast < T ? > ( ) . ToArray ( ) ;
213
+ }
196
214
}
197
- else
198
- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
199
215
}
200
216
201
- public static object GetGriddlyDefault ( this WebViewPage page , string field )
217
+ public static object GetGriddlyParameter ( this WebViewPage page , string field )
202
218
{
203
- return page . ViewData [ "_griddlyDefault_" + field ] ;
204
- }
219
+ object value = null ;
205
220
206
- public static void ForceGriddlyDefault ( this Controller controller , string field , object value )
207
- {
208
- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
221
+ if ( ( page . ViewData [ _contextKey ] as GriddlyContext ) ? . Parameters . TryGetValue ( field , out value ) != true )
222
+ value = null ;
223
+
224
+ return value ;
209
225
}
210
226
211
227
public static Dictionary < string , object > GetGriddlyDefaults ( this WebViewPage page )
212
228
{
213
229
Dictionary < string , object > defaults = new Dictionary < string , object > ( ) ;
230
+ var context = page . ViewData [ _contextKey ] as GriddlyContext ;
214
231
215
- foreach ( var key in page . ViewData . Keys . Where ( k => k . StartsWith ( "_griddlyDefault_" ) ) )
232
+ if ( context != null )
216
233
{
217
- var value = page . ViewData [ key ] ;
218
- string stringValue = null ;
219
-
220
- Type t = null ;
221
-
222
- if ( value != null )
234
+ // TODO: is there any reason to make a new dict vs using the same one? nobody else uses it, right?
235
+ foreach ( var pair in context . Defaults )
223
236
{
224
- t = value . GetType ( ) ;
237
+ var value = pair . Value ;
225
238
226
- if ( t . IsArray )
227
- {
228
- t = t . GetElementType ( ) ;
239
+ if ( value != null )
240
+ {
241
+ Type t = value . GetType ( ) ;
242
+
243
+ if ( t . IsArray )
244
+ {
245
+ t = t . GetElementType ( ) ;
229
246
230
- if ( ( Nullable . GetUnderlyingType ( t ) ?? t ) . IsEnum )
231
- value = ( ( Array ) value ) . Cast < object > ( ) . Select ( x => x ? . ToString ( ) ) . ToArray ( ) ;
247
+ if ( ( Nullable . GetUnderlyingType ( t ) ?? t ) . IsEnum )
248
+ value = ( ( Array ) value ) . Cast < object > ( ) . Select ( x => x ? . ToString ( ) ) . ToArray ( ) ;
249
+ }
232
250
}
233
251
234
- if ( stringValue == null )
235
- stringValue = value . ToString ( ) ;
252
+ defaults [ pair . Key ] = value ;
236
253
}
237
-
238
- defaults [ key . Substring ( "_griddlyDefault_" . Length ) ] = value ;
239
254
}
240
255
241
256
return defaults ;
242
257
}
243
258
259
+ public static GriddlyContext GetOrCreateGriddlyContext ( this ControllerBase controller )
260
+ {
261
+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
262
+
263
+ if ( context == null )
264
+ {
265
+ context = new GriddlyContext ( )
266
+ {
267
+ Name = ( controller . GetType ( ) . Name + "_" + controller . ControllerContext . RouteData . GetRequiredString ( "action" ) ) . ToLower ( )
268
+ } ;
269
+
270
+ // NOTE: for 2020 Chris... yes, this is unique for multiple griddlies on a page as it is in the grid action context of each one
271
+ controller . ViewData [ _contextKey ] = context ;
272
+ }
273
+
274
+ return context ;
275
+ }
276
+
244
277
static IDictionary < string , object > ObjectToDictionary ( object value )
245
278
{
246
279
if ( value == null )
0 commit comments