I'm trying to display a monthly report of how many times each user performed some action but I'm having trouble with how to filter or load the grid data. The grid has two bound columns, user name and total count, there are two drop downs for the month and year selection, and a button that triggers the "filter" operation on the datasource.
Grid:
@(Html.Kendo().Grid<My.App.UserCount>()
.Name("UsageReportGrid")
.Columns(columns =>
{
columns.Bound(p => p.DisplayName).Title("Name");
columns.Bound(p => p.TotalCount).Title("TotalCount");
})
.DataSource(dataSource => dataSource
.Ajax()
.Read(r => r.Action("GetUsageReportData", "Reports"))
)
)
Filter invocation:
function getUsageReport(e) {
var monthValue = $("#reportMonth").val();
var yearValue = $("#reportYear").val();
var filter = { logic: "and", filters: [] };
if (monthValue && yearValue) {
filter.filters.push({ field: "reportMonth", operator: "eq", value: monthValue });
filter.filters.push({ field: "reportYear", operator: "eq", value: yearValue });
$("#UsageReportGrid").data("kendoGrid").dataSource.filter(filter);
}
}
Controller method:
public JsonResult GetUsageReportData([DataSourceRequest]DataSourceRequest request)
{
int year = GetYear(request);
int month = GetMonth(request);
List<UserCount> result = null;
if (year > 0 && month > 0)
{
DateTime startDate = new DateTime(year, month, 1);
DateTime endDate = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
result = _controller.GetUsageReport(startDate, endDate);
}
return this.Json(result.ToDataSourceResult(request));
}
UserCount class:
public class UserCount
{
public int TotalCount { get; set; }
public string DisplayName { get; set; }
}
However, when I try running this, I get this error: "System.ArgumentException: 'Invalid property or field - 'reportMonth' for type: UserCount'". I assume that is because there is no "reportMonth" property on the UserCount class but the ToDataSourceResult method attempts to use that property anyway. Is there a way to change how the mapping is done from ToDataSourceResult()? How else can I accomplish this task?