Filter for nullable DateTime?

Jul 8, 2013 at 4:09 PM
How should one bind a filter to a Nullable<DateTime> property?

If I use Html.DataFilterBuilder + filter.TypedTextBoxFor then I get an error when the expression is being built.

If I use Html.DataFilterBuilder + filter.DateTimeFor().DateCalendar() then the textbox automatically is filled in with today's date, and there's no way to remove it (without changing MVCCT code).
Jul 8, 2013 at 5:27 PM
Edited Jul 8, 2013 at 5:28 PM
Well, it looks like I can get around the error in the first case by doing this in the IFilterDescription<>:
        var res = new FilterBuilder<ViewModel>()
            .Add(Birthdate != null, FilterCondition.Equal, m => m.Birthdate.Value, Birthdate)
Rather than this:
        var res = new FilterBuilder<ViewModel>()
            .Add(Birthdate != null, FilterCondition.Equal, m => m.Birthdate, Birthdate)
Note the ".Value" property.
Jul 9, 2013 at 6:43 AM
DateTimeFor cannot represent the null value. So you either use the TypedTextBox, or convert your nullable into a boolean (value/novalue) + a not nullable type and use a checkbox, as explained here.

Anyway, your solution for the filter is the right way. The condition, in the Add statement is exactly fo this! When a filter condition is provided it is assumed not null, so it works just on not nullable values. The filter compiles intom lambda expressions, and a lambda expression with a null is not ignore...but simply fails!

The DataFilterClauseFor, instead, ignores null values, so you can use them to represent conditions to be ignored! The reason of this different behaviour is that the DataFilterClauseFor has no other way to say that the user has not provided a filter value, while the FilterBuilder has the condition of the Add statement to represent this.