22using System ;
33using System . Collections ;
44using System . Collections . Generic ;
5+ using System . Collections . Specialized ;
56using System . Linq ;
67using System . Reflection ;
78using System . Text ;
@@ -162,55 +163,50 @@ public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dict
162163
163164 public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T parameter , string field , T value )
164165 {
165- var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
166+ var context = controller . GetOrCreateGriddlyContext ( ) ;
166167
167- if ( context != null )
168- {
169- context . Defaults [ field ] = value ;
168+ context . Defaults [ field ] = value ;
170169
171- if ( controller . ControllerContext . IsChildAction
172- && ! context . IsDefaultSkipped
173- && EqualityComparer < T > . Default . Equals ( parameter , default ( T ) ) )
174- {
175- parameter = value ;
170+ if ( controller . ControllerContext . IsChildAction
171+ && ! context . IsDefaultSkipped
172+ && EqualityComparer < T > . Default . Equals ( parameter , default ( T ) ) )
173+ {
174+ parameter = value ;
176175
177- context . Parameters [ field ] = value ;
178- }
176+ context . Parameters [ field ] = parameter ;
179177 }
180178 }
181179
182180 public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T [ ] parameter , string field , IEnumerable < T > value )
183181 {
184- var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
182+ var context = controller . GetOrCreateGriddlyContext ( ) ;
185183
186- if ( context != null )
184+ context . Defaults [ field ] = value ;
185+
186+ if ( controller . ControllerContext . IsChildAction
187+ && ! context . IsDefaultSkipped
188+ && parameter == null )
187189 {
188- context . Defaults [ field ] = value ;
190+ parameter = value . ToArray ( ) ;
189191
190- if ( controller . ControllerContext . IsChildAction
191- && ! context . IsDefaultSkipped
192- && parameter == null )
193- {
194- parameter = value . ToArray ( ) ;
195- }
192+ context . Parameters [ field ] = parameter ;
196193 }
197194 }
198195
199196 public static void SetGriddlyDefault < T > ( this ControllerBase controller , ref T ? [ ] parameter , string field , IEnumerable < T > value )
200197 where T : struct
201198 {
202- var context = controller . ViewData [ _contextKey ] as GriddlyContext ;
199+ var context = controller . GetOrCreateGriddlyContext ( ) ;
203200
204- if ( context != null )
201+ context . Defaults [ field ] = value ;
202+
203+ if ( controller . ControllerContext . IsChildAction
204+ && ! context . IsDefaultSkipped
205+ && parameter == null )
205206 {
206- context . Defaults [ field ] = value ;
207+ parameter = value . Cast < T ? > ( ) . ToArray ( ) ;
207208
208- if ( controller . ControllerContext . IsChildAction
209- && ! context . IsDefaultSkipped
210- && parameter == null )
211- {
212- parameter = value . Cast < T ? > ( ) . ToArray ( ) ;
213- }
209+ context . Parameters [ field ] = parameter ;
214210 }
215211 }
216212
@@ -262,9 +258,31 @@ public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase contr
262258
263259 if ( context == null )
264260 {
261+ SortField [ ] sortFields = null ;
262+ GriddlyExportFormat ? exportFormat ;
263+
264+ NameValueCollection items = new NameValueCollection ( controller . ControllerContext . HttpContext . Request . Params ) ;
265+
266+ if ( ! int . TryParse ( items [ "pageNumber" ] , out int pageNumber ) )
267+ pageNumber = 0 ;
268+
269+ if ( ! int . TryParse ( items [ "pageSize" ] , out int pageSize ) )
270+ pageSize = 20 ;
271+
272+ if ( Enum . TryParse ( items [ "exportFormat" ] , true , out GriddlyExportFormat exportFormatValue ) )
273+ exportFormat = exportFormatValue ;
274+ else
275+ exportFormat = null ;
276+
277+ sortFields = GriddlyResult . GetSortFields ( items ) ;
278+
265279 context = new GriddlyContext ( )
266280 {
267- Name = ( controller . GetType ( ) . Name + "_" + controller . ControllerContext . RouteData . GetRequiredString ( "action" ) ) . ToLower ( )
281+ Name = ( controller . GetType ( ) . Name + "_" + controller . ControllerContext . RouteData . GetRequiredString ( "action" ) ) . ToLower ( ) ,
282+ PageNumber = pageNumber ,
283+ PageSize = pageSize ,
284+ ExportFormat = exportFormat ,
285+ SortFields = sortFields
268286 } ;
269287
270288 // 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
@@ -274,6 +292,37 @@ public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase contr
274292 return context ;
275293 }
276294
295+ // TODO: keep in sync with Extensions.GetFormattedValue
296+ public static string [ ] GetFormattedValueByType ( object value )
297+ {
298+ if ( value != null )
299+ {
300+ var type = value . GetType ( ) ;
301+
302+ if ( value is IEnumerable enumerable && type != typeof ( string ) )
303+ return enumerable . Cast < object > ( ) . Select ( x => x . ToString ( ) ) . ToArray ( ) ;
304+
305+ string stringValue ;
306+
307+ if ( type == typeof ( float ) ||
308+ type == typeof ( double ) ||
309+ type == typeof ( decimal ) )
310+ stringValue = string . Format ( "{0:n2}" , value ) ;
311+ else if ( type == typeof ( DateTime ) || type . HasCastOperator < DateTime > ( ) )
312+ stringValue = string . Format ( "{0:d}" , value ) ;
313+ else if ( type == typeof ( bool ) )
314+ stringValue = value . ToString ( ) . ToLower ( ) ;
315+ else
316+ stringValue = value . ToString ( ) ;
317+
318+ if ( ! string . IsNullOrWhiteSpace ( stringValue ) )
319+ return new [ ] { stringValue } ;
320+ }
321+
322+ return null ;
323+ }
324+
325+
277326 static IDictionary < string , object > ObjectToDictionary ( object value )
278327 {
279328 if ( value == null )
0 commit comments