Skip to content

Commit 3f534c6

Browse files
committed
Fixing conversion of nullable types in filter conversions
1 parent 8b98c7d commit 3f534c6

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
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));

0 commit comments

Comments
 (0)