Skip to content

Commit f14fa45

Browse files
committed
NetCore project
1 parent ca9726e commit f14fa45

File tree

158 files changed

+38496
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+38496
-24
lines changed

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,28 @@ public static string CurrencySymbol
4444
#if NET45
4545
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName)
4646
#else
47-
public static IHtmlContent Griddly(this IHtmlHelper htmlHelper, string actionName)
47+
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName)
4848
#endif
4949
{
50-
return htmlHelper.Griddly(actionName, null);
50+
return await htmlHelper.Griddly(actionName, null);
5151
}
5252

5353
#if NET45
5454
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName, object routeValues)
5555
#else
56-
public static IHtmlContent Griddly(this IHtmlHelper htmlHelper, string actionName, object routeValues)
56+
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName, object routeValues)
5757
#endif
5858
{
59-
return htmlHelper.Griddly(actionName, null, routeValues);
59+
return await htmlHelper.Griddly(actionName, null, routeValues);
6060
}
6161

6262
#if NET45
6363
public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionName, string controllerName)
6464
#else
65-
public static IHtmlContent Griddly(this IHtmlHelper htmlHelper, string actionName, string controllerName)
65+
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName, string controllerName)
6666
#endif
6767
{
68-
return htmlHelper.Griddly(actionName, controllerName, null);
68+
return await htmlHelper.Griddly(actionName, controllerName, null);
6969
}
7070

7171
#if NET45
@@ -75,10 +75,10 @@ public static MvcHtmlString Griddly(this HtmlHelper htmlHelper, string actionNam
7575
return htmlHelper.Action(actionName, controllerName, routeValues);
7676
}
7777
#else
78-
public static IHtmlContent Griddly(this IHtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)
78+
public static async Task<IHtmlContent> Griddly(this IHtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)
7979
{
8080
// TODO: validate that we got a GriddlyResult
81-
return htmlHelper.RenderAction(actionName, controllerName, routeValues);
81+
return await htmlHelper.RenderAction(actionName, controllerName, routeValues);
8282
}
8383
#endif
8484

@@ -571,7 +571,11 @@ static IDictionary<string, object> ObjectToDictionary(object value)
571571
.ToDictionary(p => p.Name, p => p.GetValue(value, null));
572572
}
573573

574+
#if NET45
574575
public static string Current(this UrlHelper helper, object routeValues = null, bool includeQueryString = false)
576+
#else
577+
public static string Current(this IUrlHelper helper, object routeValues = null, bool includeQueryString = false)
578+
#endif
575579
{
576580
RouteValueDictionary values = new RouteValueDictionary();
577581
StringBuilder arrayVals = new StringBuilder();

Griddly.Mvc/GriddlyResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ public override async Task ExecuteResultAsync(ActionContext context)
143143
#if NET45
144144
ViewData = new ViewDataDictionary(result),
145145
#else
146-
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()),
146+
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = result },
147147
#endif
148-
ViewName = ViewName
148+
ViewName = ViewName,
149149
};
150150

151151
#if NET45

Griddly.Mvc/GriddlySelectColumn.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Collections.Generic;
44
using System.Linq.Expressions;
55
using System.Web;
6+
using System.IO;
7+
using System.Text;
8+
using System.Text.Encodings.Web;
69
#if NET45
710
using System.Web.Mvc;
811
using System.Web.Routing;
@@ -74,8 +77,12 @@ public override HtmlString RenderCell(object row, GriddlySettings settings, bool
7477
#if NET45
7578
return new HtmlString(input.ToString(TagRenderMode.SelfClosing));
7679
#else
77-
input.TagRenderMode = TagRenderMode.SelfClosing;
78-
return new HtmlString(input.ToString());
80+
var sb = new StringBuilder();
81+
using (TextWriter tw = new StringWriter(sb))
82+
{
83+
input.RenderSelfClosingTag().WriteTo(tw, HtmlEncoder.Default);
84+
return new HtmlString(sb.ToString());
85+
}
7986
#endif
8087
}
8188
else

Griddly.Mvc/GriddlySettings.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Specialized;
44
using System.Linq;
55
using System.Linq.Expressions;
6+
using System.Reflection;
67
using System.Web;
78
#if NET45
89
using System.Web.Helpers;
@@ -81,7 +82,9 @@ public GriddlySettings(IHtmlHelper html)
8182
#endif
8283

8384
{
85+
#if NET45
8486
IdProperty = "Id";
87+
#endif
8588

8689
Columns = new List<GriddlyColumn>();
8790
Filters = new List<GriddlyFilter>();
@@ -120,7 +123,11 @@ public static void ConfigureBootstrap4Defaults()
120123
public IHtmlHelper Html { get; set; }
121124
#endif
122125
public string[] DefaultRowIds { get; set; }
126+
#if NET45
123127
public string IdProperty { get; set; }
128+
#else
129+
public abstract object TryGetId(object row);
130+
#endif
124131
public string Title { get; set; }
125132
public string ClassName { get; set; }
126133
public string TableClassName { get; set; }
@@ -424,7 +431,38 @@ public SortField[] DefaultSort
424431
public class GriddlySettings<TRow> : GriddlySettings
425432
{
426433
#if !NET45
434+
static MethodInfo _idPropGetter = typeof(TRow).GetProperty("Id", BindingFlags.IgnoreCase)?.GetGetMethod();
435+
427436
public GriddlySettings(IHtmlHelper html) : base(html) { }
437+
438+
public GriddlySettings<TRow> Id(Expression<Func<TRow, object>> expression)
439+
{
440+
IdProperty = expression.Compile();
441+
return this;
442+
}
443+
public Func<TRow, object> IdProperty { get; set; }
444+
public override object TryGetId(object row)
445+
{
446+
if (IdProperty != null)
447+
{
448+
return IdProperty((TRow)row);
449+
}
450+
else if(_idPropGetter != null)
451+
{
452+
try
453+
{
454+
return _idPropGetter.Invoke(row, null);
455+
}
456+
catch
457+
{
458+
return null;
459+
}
460+
}
461+
else
462+
{
463+
return null;
464+
}
465+
}
428466
#endif
429467

430468
public new Func<GriddlySettings<TRow>, object> FilterTemplate

Griddly.Mvc/NetCore/HtmlHelperViewExtensions.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,23 @@ namespace Griddly.Mvc
1414
{
1515
public static class HtmlHelperViewExtensions
1616
{
17-
public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, object parameters = null)
17+
public static async Task<IHtmlContent> RenderAction(this IHtmlHelper helper, string action, string controller = null, object parameters = null)
1818
{
19-
var controller = (string)helper.ViewContext.RouteData.Values["controller"];
20-
return RenderAction(helper, action, controller, parameters);
21-
}
19+
if (controller == null)
20+
controller = (string)helper.ViewContext.RouteData.Values["controller"];
2221

23-
public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, string controller, object parameters = null)
24-
{
2522
var area = (string)helper.ViewContext.RouteData.Values["area"];
26-
return RenderAction(helper, action, controller, area, parameters);
23+
return await RenderAction(helper, action, controller, area, parameters);
2724
}
2825

29-
public static IHtmlContent RenderAction(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)
26+
public static async Task<IHtmlContent> RenderAction(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)
3027
{
3128
if (action == null)
32-
throw new ArgumentNullException(nameof(controller));
33-
if (controller == null)
3429
throw new ArgumentNullException(nameof(action));
30+
if (controller == null)
31+
throw new ArgumentNullException(nameof(controller));
3532

36-
var task = RenderActionAsync(helper, action, controller, area, parameters);
37-
return task.Result;
33+
return await RenderActionAsync(helper, action, controller, area, parameters);
3834
}
3935

4036
private static async Task<IHtmlContent> RenderActionAsync(this IHtmlHelper helper, string action, string controller, string area, object parameters = null)
@@ -51,6 +47,7 @@ private static async Task<IHtmlContent> RenderActionAsync(this IHtmlHelper helpe
5147
var routeValues = new RouteValueDictionary(new { area, controller, action });
5248
var newHttpContext = httpContextFactory.Create(currentHttpContext.Features);
5349
newHttpContext.Items["IsChildAction"] = true;
50+
newHttpContext.Items["ParentActionViewContext"] = helper.ViewContext;
5451

5552
newHttpContext.Response.Body = new MemoryStream();
5653

Griddly.Mvc/NetCore/NetCoreExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Html;
44
using Microsoft.AspNetCore.Http;
55
using Microsoft.AspNetCore.Mvc.Razor;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
67
using Microsoft.Extensions.Primitives;
78
using System.Collections.Specialized;
89
using System.IO;
@@ -55,6 +56,10 @@ public static bool IsChildAction(this HttpContext context)
5556
{
5657
return context.Items["IsChildAction"] != null && (bool)context.Items["IsChildAction"];
5758
}
59+
public static ViewContext ParentActionViewContext(this HttpContext context)
60+
{
61+
return context.Items["IsChildAction"] != null && (bool)context.Items["IsChildAction"] ? (context.Items["ParentActionViewContext"] as ViewContext) : null;
62+
}
5863
}
5964
}
6065

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Faker;
2+
using Griddly.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace Griddly.Controllers
9+
{
10+
public partial class ExampleController : Controller
11+
{
12+
public IActionResult Columns()
13+
{
14+
return View("Example", new ExampleModel(_env)
15+
{
16+
Title = "Columns Example",
17+
GridAction = "ColumnsGrid",
18+
ParentView = "Columns.cshtml",
19+
GridView = "ColumnsGrid.cshtml",
20+
Description = "Griddly column helpers offer several ways to quickly define your table structure."
21+
});
22+
}
23+
24+
public IActionResult Buttons(string lastName = null)
25+
{
26+
ViewBag.LastName = lastName;
27+
28+
return View("Example", new ExampleModel(_env)
29+
{
30+
Title = "Buttons Example",
31+
GridAction = "ButtonsGrid",
32+
ParentView = "Buttons.cshtml",
33+
GridView = "ButtonsGrid.cshtml",
34+
Description = ""
35+
});
36+
}
37+
38+
public IActionResult Filters()
39+
{
40+
return View("Example", new ExampleModel(_env)
41+
{
42+
Title = "Filters Example",
43+
GridAction = "FiltersGrid",
44+
ParentView = "Filters.cshtml",
45+
GridView = "FiltersGrid.cshtml",
46+
Description = "There are several filter helpers built into Griddly. Click the \"Filter\" button to play with these."
47+
});
48+
}
49+
50+
public IActionResult FilterDefaults()
51+
{
52+
return View("Example", new ExampleModel(_env)
53+
{
54+
Title = "Filter Defaults Example",
55+
GridAction = "FilterDefaultsGrid",
56+
ParentView = "FilterDefaults.cshtml",
57+
GridView = "FilterDefaultsGrid.cshtml",
58+
Description = "Filter default values may be set using the ControllerBase.SetGriddlyDefault() extension method."
59+
});
60+
}
61+
62+
public IActionResult Parameters()
63+
{
64+
return View("Example", new ExampleModel(_env)
65+
{
66+
Title = "Additional Parameters Example",
67+
GridAction = "ParametersGrid",
68+
ParentView = "Parameters.cshtml",
69+
GridView = "ParametersGrid.cshtml",
70+
Description = "Non-filter parameters may be passed from the parent view to the grid action on every refresh request."
71+
});
72+
}
73+
74+
#region Test Data
75+
76+
static readonly IList<TestGridItem> _testData = BuildTestData();
77+
78+
static List<TestGridItem> BuildTestData(int rows = 1000)
79+
{
80+
List<TestGridItem> items = new List<TestGridItem>();
81+
Random r = new Random();
82+
83+
for (int i = 0; i < rows; i++)
84+
{
85+
items.Add(new TestGridItem()
86+
{
87+
Id = (long)i,
88+
FirstName = Name.First(),
89+
LastName = Name.Last(),
90+
Company = Company.Name(),
91+
Address = r.Next(short.MaxValue) + " " + Address.StreetName(),
92+
City = Address.City(),
93+
State = Address.UsState(),
94+
PostalCode = Address.ZipCode(),
95+
Quantity = 1 + r.Next(10),
96+
Total = 1 + (decimal)(r.NextDouble() * 10000),
97+
IsApproved = r.Next(10) > 3,
98+
Date = GetRandomDate(r, DateTime.Today.AddYears(-10), DateTime.Today.AddYears(10)),
99+
Item = "Item" + (1 + r.Next(9)),
100+
101+
Test = (decimal)(r.NextDouble() * 10000),
102+
NullThing = (string)null,
103+
});
104+
}
105+
106+
return items;
107+
}
108+
109+
static DateTime GetRandomDate(Random r, DateTime from, DateTime to)
110+
{
111+
var range = to - from;
112+
113+
var randTimeSpan = new TimeSpan((long)(r.NextDouble() * range.Ticks));
114+
115+
return from + randTimeSpan;
116+
}
117+
118+
#endregion
119+
}
120+
}

0 commit comments

Comments
 (0)