Skip to content

Commit 364f84f

Browse files
committed
Fill in the blanks for the .NET core implementation
1 parent f14fa45 commit 364f84f

20 files changed

+299
-158
lines changed

Griddly.Mvc/GriddlyCookieFilterValueProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override IValueProvider GetValueProvider(ControllerContext controllerCont
7979
// chrome/ff don't delete session cookies if they're set to "continue where you left off"
8080
// https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
8181
// only use a cookie if it's new within 100 minutes
82-
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMilliseconds < 100)
82+
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMinutes < 100)
8383
{
8484
context.CookieData = data;
8585
context.IsDefaultSkipped = true;
@@ -112,7 +112,7 @@ public Task CreateValueProviderAsync(ValueProviderFactoryContext vpfc)
112112
{
113113
return Task.Factory.StartNew(() =>
114114
{
115-
var isChildAction = vpfc.ActionContext.HttpContext.Items.ContainsKey("IsChildAction");
115+
var isChildAction = vpfc.ActionContext.HttpContext.IsChildAction();
116116

117117
if (isChildAction && vpfc.ActionContext.HttpContext.Request.Query.Count == 0)
118118
{
@@ -130,7 +130,7 @@ public Task CreateValueProviderAsync(ValueProviderFactoryContext vpfc)
130130
// chrome/ff don't delete session cookies if they're set to "continue where you left off"
131131
// https://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies
132132
// only use a cookie if it's new within 100 minutes
133-
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMilliseconds < 100)
133+
if (data.CreatedUtc != null && (DateTime.UtcNow - data.CreatedUtc.Value).TotalMinutes < 100)
134134
{
135135
context.CookieData = data;
136136
context.IsDefaultSkipped = true;

Griddly.Mvc/GriddlyExport.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,54 @@
1212
#else
1313
using Microsoft.AspNetCore.Mvc.ModelBinding;
1414
using Microsoft.AspNetCore.Mvc.ViewFeatures;
15-
using Microsoft.Extensions.DependencyInjection;
1615
using Microsoft.AspNetCore.Mvc.Rendering;
1716
#endif
1817

1918
namespace Griddly.Mvc
2019
{
2120
public class GriddlyExport
2221
{
22+
#if NET45
2323
public GriddlyExport(string name, bool useGridColumns = false)
24+
#else
25+
public GriddlyExport(IHtmlHelper html, string name, bool useGridColumns = false)
26+
#endif
27+
2428
{
2529
this.UseGridColumns = useGridColumns;
2630
this.Name = name;
2731
this.Columns = new List<GriddlyColumn>();
32+
#if !NET45
33+
this.Html = html;
34+
#endif
2835
}
2936
public string Name { get; set; }
3037
public bool UseGridColumns { get; set; }
3138
public List<GriddlyColumn> Columns { get; set; }
39+
#if !NET45
40+
public IHtmlHelper Html { get; set; }
41+
#endif
3242
}
3343
public class GriddlyExport<TRow> : GriddlyExport
3444
{
45+
#if NET45
3546
public GriddlyExport(string name, bool useGridColumns = false)
3647
: base(name, useGridColumns)
48+
#else
49+
public GriddlyExport(IHtmlHelper html, string name, bool useGridColumns = false)
50+
: base(html, name, useGridColumns)
51+
#endif
3752
{
3853
}
39-
public GriddlyExport<TRow> Column<TProperty>(
40-
#if !NET45
41-
IHtmlHelper html,
42-
#endif
43-
Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null, bool visible = true, string columnId = null)
54+
public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null, bool visible = true, string columnId = null)
4455
{
45-
ModelMetadata metadata = null;
46-
4756
if (expression != null)
4857
{
4958
#if NET45
50-
metadata = ModelMetadata.FromLambdaExpression<TRow, TProperty>(expression, new ViewDataDictionary<TRow>());
59+
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TRow, TProperty>(expression, new ViewDataDictionary<TRow>());
5160
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
5261
#else
53-
var metadataProvider = html.ViewContext.HttpContext.RequestServices.GetService<ModelExpressionProvider>();
54-
var modelExpression = metadataProvider.CreateModelExpression(new ViewDataDictionary<TRow>(null), expression);
55-
metadata = modelExpression.Metadata;
56-
string htmlFieldName = metadata.Name;
62+
string htmlFieldName = ExpressionHelper.GetExpressionText(expression, Html, out var metadata);
5763
#endif
5864
Type type = metadata.ModelType;
5965

@@ -103,17 +109,9 @@ public GriddlyExport<TRow> Column<TProperty>(
103109
return this;
104110
}
105111

106-
public GriddlyExport<TRow> Column(
107-
#if !NET45
108-
IHtmlHelper html,
109-
#endif
110-
string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null)
112+
public GriddlyExport<TRow> Column(string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null)
111113
{
112-
return Column<object>(
113-
#if !NET45
114-
html,
115-
#endif
116-
null, caption, format, expressionString, defaultSort, className, renderMode, width, summaryFunction, summaryValue, template, htmlAttributes, headerHtmlAttributes, defaultSortOrder, value, exportWidth);
114+
return Column<object>(null, caption, format, expressionString, defaultSort, className, renderMode, width, summaryFunction, summaryValue, template, htmlAttributes, headerHtmlAttributes, defaultSortOrder, value, exportWidth);
117115
}
118116
}
119117
}

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,39 @@ public static string CurrencySymbol
4343

4444
#if NET45
4545
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName)
46+
{
47+
return htmlHelper.Griddly(actionName, null);
48+
}
4649
#else
4750
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName)
48-
#endif
4951
{
5052
return await htmlHelper.Griddly(actionName, null);
5153
}
54+
#endif
5255

5356
#if NET45
5457
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName, object routeValues)
58+
{
59+
return htmlHelper.Griddly(actionName, null, routeValues);
60+
}
5561
#else
5662
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName, object routeValues)
57-
#endif
5863
{
5964
return await htmlHelper.Griddly(actionName, null, routeValues);
6065
}
66+
#endif
6167

6268
#if NET45
6369
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName, string controllerName)
70+
{
71+
return htmlHelper.Griddly(actionName, controllerName, null);
72+
}
6473
#else
6574
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName, string controllerName)
66-
#endif
6775
{
6876
return await htmlHelper.Griddly(actionName, controllerName, null);
6977
}
78+
#endif
7079

7180
#if NET45
7281
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)
@@ -274,16 +283,20 @@ public static void SetGriddlyDefault<T>(this Controller controller, ref T parame
274283

275284
#if NET45
276285
if (controller.ControllerContext.IsChildAction
286+
#else
287+
if (controller.HttpContext.IsChildAction()
288+
#endif
277289
&& (!context.IsDefaultSkipped || ignoreSkipped.Value)
278-
&& EqualityComparer<T>.Default.Equals(parameter, default(T)))
290+
&& (EqualityComparer<T>.Default.Equals(parameter, default(T))
291+
#if !NET45
292+
|| typeof(T).IsArray && (parameter as Array)?.Length == 0 //In .NET core, the default value for array is null, but MVC binds the parameter as zero length array
293+
#endif
294+
))
279295
{
280296
parameter = value;
281297

282298
context.Parameters[field] = parameter;
283299
}
284-
#else
285-
//TODO: implement
286-
#endif
287300
}
288301

289302
#if NET45
@@ -301,16 +314,16 @@ public static void SetGriddlyDefault<T>(this Controller controller, ref T[] para
301314

302315
#if NET45
303316
if (controller.ControllerContext.IsChildAction
317+
#else
318+
if (controller.HttpContext.IsChildAction()
319+
#endif
304320
&& (!context.IsDefaultSkipped || ignoreSkipped.Value)
305321
&& parameter == null)
306322
{
307323
parameter = value.ToArray();
308324

309325
context.Parameters[field] = parameter;
310326
}
311-
#else
312-
//TODO: implement
313-
#endif
314327
}
315328

316329
#if NET45
@@ -329,19 +342,18 @@ public static void SetGriddlyDefault<T>(this Controller controller, ref T?[] par
329342

330343
#if NET45
331344
if (controller.ControllerContext.IsChildAction
345+
#else
346+
if (controller.HttpContext.IsChildAction()
347+
#endif
332348
&& (!context.IsDefaultSkipped || ignoreSkipped.Value)
333349
&& parameter == null)
334350
{
335351
parameter = value.Cast<T?>().ToArray();
336352

337353
context.Parameters[field] = parameter;
338354
}
339-
#else
340-
//TODO: implement
341-
#endif
342355
}
343356

344-
#if NET45
345357
public static void SetGriddlyDefault<TController, TModel, TProp>(this TController controller, TModel model,
346358
Expression<Func<TModel, TProp>> expression, TProp defaultValue, bool? ignoreSkipped = null)
347359
where TController : Controller
@@ -351,13 +363,21 @@ public static void SetGriddlyDefault<TController, TModel, TProp>(this TControlle
351363

352364
var context = controller.GetOrCreateGriddlyContext();
353365

366+
#if NET45
354367
var field = ExpressionHelper.GetExpressionText(expression);
368+
#else
369+
var field = ExpressionHelper.GetExpressionText(expression, controller.HttpContext);
370+
#endif
355371
context.Defaults[field] = defaultValue;
356372

357373
var compiledExpression = expression.Compile();
358374
TProp parameter = compiledExpression(model);
359375

376+
#if NET45
360377
if (controller.ControllerContext.IsChildAction
378+
#else
379+
if (controller.HttpContext.IsChildAction()
380+
#endif
361381
&& (!context.IsDefaultSkipped || ignoreSkipped.Value)
362382
&& EqualityComparer<TProp>.Default.Equals(parameter, default(TProp)))
363383
{
@@ -376,9 +396,6 @@ public static void SetGriddlyDefault<TController, TModel, TProp>(this TControlle
376396
}
377397
}
378398
}
379-
#else
380-
//TODO: implement
381-
#endif
382399

383400
#if NET45
384401
public static object GetGriddlyDefault(this WebViewPage page, string field)
@@ -454,9 +471,12 @@ public static Dictionary<string, object> GetGriddlyDefaults(this RazorPageBase p
454471
}
455472

456473
#if !NET45
457-
public static GriddlyContext GetOrCreateGriddlyContext(this ActionContext context)
474+
public static GriddlyContext GetOrCreateGriddlyContext(this ActionContext actionContext)
458475
{
459-
return GetOrCreateGriddlyContext(context.RouteData, context.HttpContext);
476+
var context = GetOrCreateGriddlyContext(actionContext.RouteData, actionContext.HttpContext);
477+
if (actionContext is ViewContext vc)
478+
vc.ViewData[_contextKey] = context;
479+
return context;
460480
}
461481

462482
public static GriddlyContext GetOrCreateGriddlyContext(this Controller controller)
@@ -472,8 +492,8 @@ public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase contr
472492
#else
473493
private static GriddlyContext GetOrCreateGriddlyContext(RouteData routeData, HttpContext httpContext)
474494
{
475-
var key = _contextKey + "_" + (routeData.Values["controller"] as string).ToLower() + "_" + (routeData.Values["action"] as string).ToLower();
476-
var context = httpContext.Items[key] as GriddlyContext;
495+
//var key = _contextKey + "_" + (routeData.Values["controller"] as string).ToLower() + "_" + (routeData.Values["action"] as string).ToLower();
496+
var context = httpContext.Items[_contextKey] as GriddlyContext;
477497
#endif
478498

479499
if (context == null)
@@ -523,7 +543,7 @@ private static GriddlyContext GetOrCreateGriddlyContext(RouteData routeData, Htt
523543
#if NET45
524544
controller.ViewData[_contextKey] = context;
525545
#else
526-
httpContext.Items[key] = context; //TODO: Review
546+
httpContext.Items[_contextKey] = context;
527547
#endif
528548
}
529549

0 commit comments

Comments
 (0)