Skip to content

Commit 0acae3f

Browse files
committed
Merge pull request mcintyre321#90 from klaforce/dev-nullable-conversion-bugfix
Dev nullable conversion bugfix
2 parents 8b98c7d + c662903 commit 0acae3f

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Mvc.JQuery.Datatables/TypeFilters.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Mvc.JQuery.Datatables
66
{
77
static class TypeFilters
88
{
9-
private static readonly Func<string, Type, object> ParseValue =
9+
private static readonly Func<string, Type, object> ParseValue =
1010
(input, t) => t.IsEnum ? Enum.Parse(t, input) : Convert.ChangeType(input, t);
11-
11+
1212
internal static string FilterMethod(string q, List<object> parametersForLinqQuery, Type type)
1313
{
1414

@@ -51,7 +51,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
5151
var clause = null as string;
5252
try
5353
{
54-
parametersForLinqQuery.Add(Convert.ChangeType(parts[0], colInfo.Type));
54+
parametersForLinqQuery.Add(ChangeType(colInfo, parts[0]));
5555
clause = string.Format("{0} >= @{1}", columnname, parametersForLinqQuery.Count - 1);
5656
}
5757
catch (FormatException)
@@ -60,7 +60,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
6060

6161
try
6262
{
63-
parametersForLinqQuery.Add(Convert.ChangeType(parts[1], colInfo.Type));
63+
parametersForLinqQuery.Add(ChangeType(colInfo, parts[1]));
6464
if (clause != null) clause += " and ";
6565
clause += string.Format("{0} <= @{1}", columnname, parametersForLinqQuery.Count - 1);
6666
}
@@ -74,7 +74,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
7474
{
7575
try
7676
{
77-
parametersForLinqQuery.Add(Convert.ChangeType(query, colInfo.Type));
77+
parametersForLinqQuery.Add(ChangeType(colInfo, query));
7878
return string.Format("{0} == @{1}", columnname, parametersForLinqQuery.Count - 1);
7979
}
8080
catch (FormatException)
@@ -84,6 +84,19 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
8484
}
8585
}
8686

87+
private static object ChangeType(ColInfo colInfo, string query)
88+
{
89+
if (colInfo.Type.IsGenericType && colInfo.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
90+
{
91+
Type u = Nullable.GetUnderlyingType(colInfo.Type);
92+
return Convert.ChangeType(query, u);
93+
}
94+
else
95+
{
96+
return Convert.ChangeType(query, colInfo.Type);
97+
}
98+
}
99+
87100
public static string DateTimeOffsetFilter(string query, string columnname, ColInfo colInfo, List<object> parametersForLinqQuery)
88101
{
89102
if (query == "~") return string.Empty;
@@ -188,7 +201,7 @@ public static string StringFilter(string q, string columnname, ColInfo columntyp
188201

189202
public static string EnumFilter(string q, string columnname, ColInfo colInfo, List<object> parametersForLinqQuery)
190203
{
191-
204+
192205
if (q.StartsWith("^")) q = q.Substring(1);
193206
if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1);
194207
parametersForLinqQuery.Add(ParseValue(q, colInfo.Type));

Mvc.Jquery.DataTables.Tests/DummyPocos/SomeModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public class SomeModel
1212
public double Scale { get; set; }
1313
public DateTime When { get; set; }
1414
public bool Discounted { get; set; }
15+
public decimal? Cost { get; set; }
1516
}
1617
}

Mvc.Jquery.DataTables.Tests/FilterTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public void Setup()
2424
DisplayName = "Cheddar",
2525
Id = 123,
2626
Scale = 123.456d,
27-
Discounted = true
27+
Discounted = true,
28+
Cost = 123
2829
}
2930
}.AsQueryable();
3031

@@ -50,6 +51,8 @@ public void Setup()
5051
[TestCase("^456$", typeof(int), false)] //exact query, isnt match
5152
[TestCase("123", typeof(int), true)] //query, is match
5253
[TestCase("456", typeof(int), false)] //query, isnt match
54+
[TestCase("123", typeof(decimal?), true)] //query, is match
55+
[TestCase("456", typeof(decimal?), false)] //query, isnt match
5356
public void SearchQueryTests(string searchString, Type colType, bool returnsResult)
5457
{
5558
var col = columns.First(c => c.Item2.Type == colType).Item1;

0 commit comments

Comments
 (0)