Nikitesh
Nikitesh

Reputation: 1305

Kendo Datasource filter not working as expected

I'm using Kendo MVC grid to display data. I have to filter the data based on the url clicked by the user. Below is the code for Kendo grid

View

@(Html.Kendo().Grid<WebApplication2.ApplicationViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.ApplicationStatus).Width(150);
            columns.Bound(c => c.StartDate).Width(150).Filterable(ftb => ftb.Cell(cell => cell.Operator("equals")));
            columns.Bound(c => c.EndDate).Width(150).Filterable(ftb => ftb.Cell(cell => cell.Operator("equals"))).Groupable(false);                        
        })
               .HtmlAttributes(new { style = "height: 500px;width:100%" })
                .Groupable()
       .Scrollable()
       .Filterable(ftb => ftb.Mode(Kendo.Mvc.UI.GridFilterMode.Row)) // Filter Code
       .Sortable()
       .Pageable(pageable => pageable
       .Refresh(true)
       .PageSizes(true))
       .DataSource(dataSource => dataSource
       .Ajax()
       .ServerOperation(true)
       .Read(read => read.Action("FetchData", "Home"))
                       .Filter(f => f.Add(p => p.ApplicationStatus).IsEqualTo(ViewBag.ApplicationStatus))
                       .Filter(f => f.Add(p => p.StartDate).IsGreaterThanOrEqualTo(Convert.ToDateTime(ViewBag.FromDate)))
                       .Filter(f => f.Add(p => p.StartDate).IsLessThanOrEqualTo(Convert.ToDateTime(ViewBag.ToDate)))
       .PageSize(KendoGridConstants.KendoGridDefaultPageSize)
       )
    )

Controller Code:

public ActionResult ApplicationGrid(string ApplicationStatus, string fromDate, string toDate)
        {            
            ViewBag.ApplicationStatus = ApplicationStatus == null ? "All" : ApplicationStatus;
            ViewBag.FromDate = fromDate == null ? DateTime.Now.ToShortDateString() : Convert.ToDateTime(fromDate).ToShortDateString();
            ViewBag.ToDate = toDate == null ? DateTime.Now.ToShortDateString() : Convert.ToDateTime(toDate).ToShortDateString();
            return View();
        }

        public ActionResult FetchData([DataSourceRequest]DataSourceRequest request)
        {
           var applicationList = Dbcontext.ApplicationStatus;
            return Json(applicationList.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

If I keep only one filter on StartDate it works fine but if I keep both the filters, it is showing me some irrelevant data. I need to filter on date range. Any other alternative is also fine.

Upvotes: 2

Views: 988

Answers (1)

Jonathan
Jonathan

Reputation: 5028

I know this is really old, but I had the same problem, couldn't find documentation, and ended up, as always, on Stack Overflow. So I'm contributing my solution.

Need to add additional and / or operators to the Filter statement on the DataSource:

.Read(read => read.Action("FetchData", "Home"))
  .Filter(f => f.Add(p => p.StartDate).IsGreaterThanOrEqualTo(Convert.ToDateTime(ViewBag.FromDate)))
       .And().IsLessThanOrEqualTo(Convert.ToDateTime(ViewBag.ToDate)))

Upvotes: 2

Related Questions