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

Custom Filtering on related entites

2 Answers 68 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Philip Street
Top achievements
Rank 1
Philip Street asked on 24 Jun 2013, 09:58 PM
I have the following (simplified) VMs;

public class ProjectModel
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<CountryModel> Countries { get; set; }
}

public class CountryModel
{
    public int CountryId { get; set; }
    public string Name { get; set; }
}

I am using the Grid with ajax binding to display the list of projects. I have a custom UI for the user to specify search criteria, i.e. textboxes for name and description, and a MultiSelect control so that the user can select one or more countries.

I want custom filtering against the countries - I want the search to perform an AND filter returning Projects that are associated with all the countries selected by the user.

I am currently able to capture title, description and the selected countries from the MultiSelect, and pass them to my ProjectSearch Controller Action as additional data (using the DataSource Read Data). I then create  FilterDescriptors for the project name and description, but am a bit stuck as to how to do it for the countries. 

So what is the correct way of creating the FilterDescriptors for a country search? What "member" should I specify? What operator do I use?

Many thanks for any assistance.

Phil

2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 26 Jun 2013, 01:34 PM
Hello Philip,

The Grid does not support filtering on an Array field. Only base types (string,number,booleans etc) are supported. 

You will need to filter the collection manually in the controller before passing it to the ToDataSourceResult extension where rest of the filters for the base fields will be applied automatically.

I am sorry for any inconvenience caused.

Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Accepted
Philip Street
Top achievements
Rank 1
answered on 26 Jun 2013, 01:45 PM
OK.

Since posting this I have come up with a workaround, which is to add the following property to the ProjectModel;

        public string CountryIDs 
        {
            get
            {
                return string.Format("|{0}|", string.Join("|", this.Countries.Select(c => c.CountryId).ToArray()));
            }
        }

then in my controller I create the following Country FilterDescriptor ;

            if (!string.IsNullOrEmpty(project.countries))
            {
                var countryFilter = new CompositeFilterDescriptor
                                        {
                                            LogicalOperator =
                                                FilterCompositionLogicalOperator.And
                                        };
                var countryList = countries.Split(',');
                foreach (var countryId in countryList)
                {
                    countryFilter.FilterDescriptors.Add(
                        this.CreateFilterDescriptor(
                            "CountryIDs", FilterOperator.Contains, string.Format("|{0}|", countryId)));
                }
            }

This is working for me on this and the other three or four many-to-many relationships. I hope this helps others.

Phil
Tags
Data Source
Asked by
Philip Street
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Philip Street
Top achievements
Rank 1
Share this question
or