Filter value is NULL

3 posts, 1 answers
  1. Andre Damsteegt
    Andre Damsteegt avatar
    1 posts
    Member since:
    Apr 2006

    Posted 19 Jun 2013 Link to this post

    Using the Kendo Autocomplete to search in a collection of 5000+ items stored in a database. Due to that is chosen to perform the filtering on the server. The problem is that the search value (parameter filter) received by the function is always NULL.

    The project is MVC4/razor based, on the page the autocomplete is defined like this:

    @(Html.Kendo().AutoComplete()
                      .Name(
    "ExternalId")
                      .Placeholder(
    "Search...")
                      .MinLength(3)
                      .DataSource(source => source.Read(read => read.Action(
    "SourcesForAutoComplete", "Source")
        .Data(
    "onAdditionalData"))
                      .ServerFiltering(
    true))
                      .HtmlAttributes(
    new { style = "width:468px" }))
    The javascript handling the additional data events is placed at the end of the cshtml of the page:
    <script>
        function onAdditionalData() {
            return { filter: $("#ExternalId").val() };
        }
    </script>

    The MVC Source controller has the following function to provide the data and search functionality:
    public JsonResult SourcesForAutoComplete(string filter)
    {
        using (var db = new EasyTaskIntegrationEntities())
        {
            ...
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
    Can anyone explain why the filter value received is NULL instead of the value typed in the Autocomplete?
  2. Answer
    Andre Damsteegt
    Andre Damsteegt avatar
    1 posts
    Member since:
    Apr 2006

    Posted 20 Jun 2013 Link to this post

    It seems the filter value is not transmitted via the parameter of function.
    The MVC Source controller has now the modified function to provide the data and search functionality: 
    public JsonResult SourcesForAutoComplete()
    {
        var searchValue = Request.Params["filter[filters][0][value]"];
        using (var db = new EasyTaskIntegrationEntities())
        {
           if (string.IsNullOrEmpty(searchValue)) searchValue = string.Empty;
           //Get max 20 results
           var result = db.Sources.Where(s => s.ExternalId.StartsWith(searchValue))
                                    .OrderBy(s => s.ExternalId)
                                    .Take(20)
                                    .Select(s => s.ExternalId)
                                    .ToArray();
     
           return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Josh
    Josh avatar
    4 posts
    Member since:
    Jul 2014

    Posted 25 Aug 2014 in reply to Andre Damsteegt Link to this post

    I'mhaving a hard time using your code for the autocomplete.   Will you post your controller and view code please?

    Thanks!
Back to Top