I am not getting any errors in my code, but my filter object is always null. When I run the debugger the filter object looks just like the sort object, a list with stuff in it. Although as you can see it is actually a interface.. What do I need to change in this code to access the information in the filter? I guess my main problem is I don't quite fully grasp how to work with interfaces.
public IList<Kendo.Mvc.IFilterDescriptor> Filters { get; set; }
public IList<Kendo.Mvc.SortDescriptor> Sorts { get; set; }
List<SortDescriptor> sort = request.Sorts as List<SortDescriptor>;
if(sort.Count == 1)
{
pRequest.SortMember = sort[0].Member;
pRequest.SortDirection = (int)sort[0].SortDirection;
}
List<FilterDescriptor> filter = request.Filters as List<FilterDescriptor>;
if (filter != null)
{
pRequest.Startdate = (DateTime)filter[0].ConvertedValue;
}
else
{
var endDate = new TimeSpan(4000, 0, 0, 0, 0);
pRequest.Startdate = DateTime.UtcNow - endDate;
}
The list is null because you are trying to cast
List<IFilterDescriptor>
intoList<FilterDescriptor>
, but just becauseFilterDescriptor
inherits fromIFilterDescriptor
doesn't mean thatList<FilterDescriptor>
inherits fromList<IFilterDescriptor>
(unless the list is covariantList<in T>
which it isn't).You should do it like this:
if the interface is fine with that, otherwise you can case the list like that:
Thanks for the help guys I talked to a friend of mine and he suggested I do this... So I guess I should not have been trying to cast the Lists and Interfaces.