@@ -158,89 +158,122 @@ public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dict
158158 return new HtmlString ( sb . ToString ( ) ) ;
159159 }
160160
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 )
162164 {
163- if ( controller . ControllerContext . IsChildAction )
165+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
166+
167+ if ( context != null )
164168 {
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+ {
166175 parameter = value ;
167176
168- controller . ViewData [ "_griddlyDefault_" + field ] = parameter ;
177+ context . Parameters [ field ] = value ;
178+ }
169179 }
170- else
171- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
172180 }
173181
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 )
175183 {
176- if ( controller . ControllerContext . IsChildAction )
184+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
185+
186+ if ( context != null )
177187 {
178- if ( parameter == null )
179- parameter = value . ToArray ( ) ;
188+ context . Defaults [ field ] = value ;
180189
181- controller . ViewData [ "_griddlyDefault_" + field ] = parameter ;
190+ if ( controller . ControllerContext . IsChildAction
191+ && ! context . IsDefaultSkipped
192+ && parameter == null )
193+ {
194+ parameter = value . ToArray ( ) ;
195+ }
182196 }
183- else
184- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
185197 }
186198
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 )
188200 where T : struct
189201 {
190- if ( controller . ControllerContext . IsChildAction )
202+ var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
203+
204+ if ( context != null )
191205 {
192- if ( parameter == null )
193- parameter = value . Cast < T ? > ( ) . ToArray ( ) ;
206+ context . Defaults [ field ] = value ;
194207
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+ }
196214 }
197- else
198- controller . ViewData [ "_griddlyDefault_" + field ] = value ;
199215 }
200216
201- public static object GetGriddlyDefault ( this WebViewPage page , string field )
217+ public static object GetGriddlyParameter ( this WebViewPage page , string field )
202218 {
203- return page . ViewData [ "_griddlyDefault_" + field ] ;
204- }
219+ object value = null ;
205220
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 ;
209225 }
210226
211227 public static Dictionary < string , object > GetGriddlyDefaults ( this WebViewPage page )
212228 {
213229 Dictionary < string , object > defaults = new Dictionary < string , object > ( ) ;
230+ var context = page . ViewData [ _contextKey ] as GriddlyContext ;
214231
215- foreach ( var key in page . ViewData . Keys . Where ( k => k . StartsWith ( "_griddlyDefault_" ) ) )
232+ if ( context != null )
216233 {
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 )
223236 {
224- t = value . GetType ( ) ;
237+ var value = pair . Value ;
225238
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 ( ) ;
229246
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+ }
232250 }
233251
234- if ( stringValue == null )
235- stringValue = value . ToString ( ) ;
252+ defaults [ pair . Key ] = value ;
236253 }
237-
238- defaults [ key . Substring ( "_griddlyDefault_" . Length ) ] = value ;
239254 }
240255
241256 return defaults ;
242257 }
243258
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+
244277 static IDictionary < string , object > ObjectToDictionary ( object value )
245278 {
246279 if ( value == null )
0 commit comments