Skip to content

Commit c5300d4

Browse files
committed
Fix for null DateTime?s
1 parent e866e9b commit c5300d4

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

Mvc.JQuery.Datatables.Example/Controllers/FakeDatabase.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ static FakeDatabase()
2222
Position = positions[i%positions.Count],
2323
IsAdmin = i%11 == 0,
2424
Number = (Numbers) r.Next(4),
25-
Hired =
26-
DateTime.UtcNow.AddDays(-1*365*3*
27-
r.NextDouble()),
25+
Hired = i % 8 == 0 ? null as DateTime? : DateTime.UtcNow.AddDays(-1*365*3* r.NextDouble()),
2826
Salary =
2927
10000 + (DateTime.UtcNow.Minute*1000) +
3028
(DateTime.UtcNow.Second*100) +
@@ -59,7 +57,7 @@ public class User
5957

6058
public PositionTypes? Position { get; set; }
6159

62-
public DateTime Hired { get; set; }
60+
public DateTime? Hired { get; set; }
6361

6462
public Numbers Number { get; set; }
6563

Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public DataTablesResult<UserView> GetUsers(DataTablesParam dataTableParam)
3232
uv => new
3333
{
3434
Name = "<b>" + uv.Name + "</b>",
35-
Hired = uv.Hired.ToShortDateString() + " (" + FriendlyDateHelper.GetPrettyDate(uv.Hired) + ") "
35+
Hired = uv.Hired == null ? "&lt;pending&gt;" : uv.Hired.Value.ToShortDateString() + " (" + FriendlyDateHelper.GetPrettyDate(uv.Hired.Value) + ") "
3636
});
3737
}
3838

@@ -64,7 +64,7 @@ public class UserView
6464
[DataTables( Sortable = false)]
6565
public bool IsAdmin { get; set; }
6666
public string Position { get; set; }
67-
public DateTime Hired { get; set; }
67+
public DateTime? Hired { get; set; }
6868

6969
public Numbers Number { get; set; }
7070

Mvc.JQuery.Datatables/StringTransformers.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public static Dictionary<string, object> TransformDictionary(Dictionary<string,
5959
var output = new Dictionary<string, object>();
6060
foreach (var row in dict)
6161
{
62-
if (row.Value != null)
63-
{
64-
output[row.Key] = GetStringedValue(row.Value.GetType(), row.Value);
65-
}
62+
output[row.Key] = row.Value == null ? "" : GetStringedValue(row.Value.GetType(), row.Value);
6663
}
6764
return output;
6865
}

0 commit comments

Comments
 (0)