This is a migrated thread and some comments may be shown as answers.

Modelbinder for filter descriptors

5 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marijn
Top achievements
Rank 1
Marijn asked on 25 Jul 2014, 11:47 AM
I have modified the  filter-menu-customization example to perfor server side filtering based on the filter value. I created this action method:

public ActionResult FilterMenuCustomization_Titles([Bind(Prefix="filter[filters][0][value]")]string filterValue)
{
    var db = new SampleEntities();
    var employeeTitles = db.Employees
                           .Select(e => e.Title)
                           .Where(t => t.StartsWith(filterValue))
                           .Distinct();
    return Json(employeeTitles, JsonRequestBehavior.AllowGet);
}

This works fine, but the Bind attribute and the prefix string looks fragile to me. I'm pretty sure there should be a better way to perform the filterValue binding. I imagine there should be a IModelBinder for FIlterDescriptor, so that I could leverage the other aspects of the filter descriptor (starts with, equals, logic operator etc). But I failed to find it.

I posted my example here on Github.

Any suggestions?



5 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 29 Jul 2014, 06:52 AM
Hello Marijn,

From the provided information it seems that you want to use the AutoComplete widget with server filtering. How to set up this feature is demonstrated in this online demo.
And  the links (View, Controller) to the appropriate files from your git repository.

Regards,
Rosen
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Marijn
Top achievements
Rank 1
answered on 30 Jul 2014, 09:13 AM
Thanks for the reply Rosen, but that does not really answer my question; let me clarify my question.

My goal is to have the following method signature:  
public ActionResult FilterMenuCustomization_Titles(FilterDescriptor filter)
{
    // use filter in query
    // ... snip ...
}
0
Accepted
Rosen
Telerik team
answered on 30 Jul 2014, 11:29 AM
Hi Marijn,

You can take advantage the default ASP.NET MVC  model binder by formatting (by using the transport's parameterMap) the request parameters in the appropriate format. Similar to the following:

<script type="text/javascript">
    function titleFilter(element) {
        element.kendoAutoComplete({
            dataSource: {
                serverFiltering: true,
                transport: {
                    read: {
                        url: "@Url.Action("FilterMenuCustomization_Titles")",
                        type: "POST",
                        contentType: "application/json"
                    },
                    parameterMap: function (options) {
                        if (options.filter && options.filter.filters.length) {
                            return kendo.stringify(options.filter.filters[0]);
                        }
                        return options;
                    }
                }
            }
        });
    }
</script>
public ActionResult FilterMenuCustomization_Titles(FilterDescriptor filter)
{
    //...
}



Regards,
Rosen
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Marijn
Top achievements
Rank 1
answered on 30 Jul 2014, 12:59 PM
Ah, that looks great - will give it a shot and post back here.
0
Marijn
Top achievements
Rank 1
answered on 31 Jul 2014, 01:33 PM
Using parameterMap is just what I needed; working example: https://github.com/serra/Telerik-UIforASP.NETMVCQ12014-examples-VS2012/commits/grid_with_server_side_autocomplete_column_filter
Tags
Grid
Asked by
Marijn
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Marijn
Top achievements
Rank 1
Share this question
or