SfDropDownList enabling AllowFiltering using Web API

Referring to the documentation:
https://blazor.syncfusion.com/documentation/dropdown-list/data-source/#creating-web-api-controller
https://blazor.syncfusion.com/documentation/dropdown-list/data-source/#configure-dropdownlist-component-using-web-api-adaptor

I have following code:

WEB API
//GET: api/Posts
        [HttpGet]
        public object Get()
        {
            IQueryable<SimpleListdata = _db.Post.Select(x => new SimpleList { Id = x.Id, Name = x.Name }).OrderBy(x => x.Name).AsQueryable();
            var count = data.Count();
            var queryString = Request.Query;
            if (queryString.Keys.Contains("$inlinecount"))
            {
                Microsoft.Extensions.Primitives.StringValues Skip;
                Microsoft.Extensions.Primitives.StringValues Take;
                int skip = (queryString.TryGetValue("$skip"out Skip)) ? Convert.ToInt32(Skip[0]) : 0;
                int top = (queryString.TryGetValue("$top"out Take)) ? Convert.ToInt32(Take[0]) : data.Count();
                return new { Items = data.Skip(skip).Take(top), Count = count };
            }
            else
            {
                return data;
            }
        }
<SfDropDownList TValue="int" TItem="SimpleList" @bind-Value="@_modelDesignation.PostId" AllowFiltering=true  Placeholder="Select a Employee">
     <SfDataManager Url="api/post" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true">SfDataManager>
     <DropDownListFieldSettings Text="Name" Value="Id" />
SfDropDownList>
Using this method AllowFiltering is not working.


3 Replies

SP Sureshkumar P Syncfusion Team May 7, 2020 02:07 PM UTC

Hi Ashimaz, 
 
Greetings from Syncfusion support. 
 
Based on your shared information with code example. We suspect that you want to filter the loaded data in dropdownlist component. But you have not handled the filter action in your server-side code.  We suggest you handle the filter action in your server side.  
 
Kindly refer the below code example. 
 
<SfDropDownList TValue="string" TItem="Orders" AllowFiltering=true Placeholder="Select a Employee"> 
    <SfDataManager  Url="api/Default" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true"> 
    </SfDataManager> 
    <DropDownListFieldSettings Text="CustomerID" Value="CustomerID" /> 
</SfDropDownList> 
 
[controller] 
 
// GET: api/Default 
        [HttpGet] 
        public async Task<object> Get(int? code) 
        { 
            if (order.Count == 0) 
            { 
                BindDataSource(); 
            } 
            var data = order.AsQueryable();             
            var queryString = Request.Query;   
            string filter = queryString["$filter"]; 
            string auto = queryString["$inlineCount"]; 
            if (filter != null) // to handle filter opertaion 
            { 
                var newfiltersplits = filter; 
                var filtersplits = newfiltersplits.Split('(' , ')', ' ', '\''); 
                var filterfield = filtersplits[2]; 
                var filtervalue = filtersplits[4]; 
 
                if (filtersplits.Length == 7) 
                { 
                    if (filtersplits[2] == "tolower") 
                    { 
                        filterfield = filter.Split('(', ')', '\'')[3]; 
                        filtervalue = filter.Split('(', ')', '\'')[5]; 
                    } 
                } 
                switch (filterfield) 
                { 
                    case "CustomerID": 
                        data = (from cust in data 
                                where cust.CustomerID.ToLower().StartsWith(filtervalue.ToString()) 
                                select cust); 
                        break; 
                } 
            } 
            if (queryString.Keys.Contains("$inlinecount")) 
            { 
                StringValues Skip; 
                StringValues Take; 
                int skip = (queryString.TryGetValue("$skip", out Skip)) ? Convert.ToInt32(Skip[0]) : 0; 
                int top = (queryString.TryGetValue("$top", out Take)) ? Convert.ToInt32(Take[0]) : data.Count(); 
                var count = data.Count(); 
                return new { Items = data.Skip(skip).Take(top), Count = count }; 
            } 
            else 
            { 
                return data; 
            } 
        } 
 
We have created the sample based on your requirement. please find the sample here: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebAPI2111775963  
 
Could you please check the above sample and let us know whether this is fulfilling your requirement or get back to us if you need further assistance. 
 
Regards, 
Sureshkumar P 



AS ashimaz May 8, 2020 07:11 AM UTC

Provided solution is working, thank you.

Full code for reference.
//GET: api/Posts
        [HttpGet]
        public object Get()
        {
            IQueryable<SimpleListdata = _db.Post.Select(x => new SimpleList { Id = x.Id, Name = x.Name }).OrderBy(x => x.Name).AsQueryable();
            var count = data.Count();
            var queryString = Request.Query;
            
            string filter = queryString["$filter"];
 
            if (filter != null// to handle filter opertaion 
            {
                var newfiltersplits = filter;
                var filtersplits = newfiltersplits.Split('('')'' ''\'');
                var filterfield = filtersplits[2];
                var filtervalue = filtersplits[4];
 
                if (filtersplits.Length == 7)
                {
                    if (filtersplits[2== "tolower")
                    {
                        filterfield = filter.Split('('')''\'')[3];
                        filtervalue = filter.Split('('')''\'')[5];
                    }
                }
                switch (filterfield)
                {
                    case "Name":
                        data = data.Where(x => x.Name.ToLower(CultureInfo.CurrentCulture).StartsWith(filtervalue.ToString(),StringComparison.CurrentCulture));
                        break;
                }
            }
 
            if (queryString.Keys.Contains("$inlinecount"))
            {
                Microsoft.Extensions.Primitives.StringValues Skip;
                Microsoft.Extensions.Primitives.StringValues Take;
                int skip = (queryString.TryGetValue("$skip"out Skip)) ? Convert.ToInt32(Skip[0],CultureInfo.CurrentCulture) : 0;
                int top = (queryString.TryGetValue("$top"out Take)) ? Convert.ToInt32(Take[0], CultureInfo.CurrentCulture) : data.Count();
                return new { Items = data.Skip(skip).Take(top), Count = count };
            }
            else
            {
                return data;
            }
        }
<SfDropDownList TValue="int" TItem="SimpleList" @bind-Value="@_modelDesignation.PostId" AllowFiltering="true" Placeholder="Select a Post">
                                   <SfDataManager Url="api/PostSimpleList" Adaptor="Adaptors.WebApiAdaptor" CrossDomain="true">SfDataManager>
                                   <DropDownListFieldSettings Text="Name" Value="Id" />
                               SfDropDownList>


SP Sureshkumar P Syncfusion Team May 11, 2020 05:37 AM UTC

Hi Ashimaz, 
 
Thanks for your update. Please get back to us if you need further assistance on this. 
 
Regards, 
Sureshkumar P 


Loader.
Up arrow icon