We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

AutoComplete With WebApi

Dear Syncfusion, 


I want to fill autocomplete using web API. I followed this thread https://www.syncfusion.com/forums/124804/searching-existing-api-using-autocomplete
but unfortunately, it seems doesn't work for EJ2. 


 function OnCustomerChange(args) {
            if (args.value != "") {
                $.ajax({
                    type: "GET",
                    url: "https://localhost:5201/api/v1/Customer/WithName/" + args.value,
                    contentType: "application/json; charset=utf-8",
                    crossdomain: true,
                    dataType: "json",
                    success: OnCustomerAjaxSuccess,
                    failure: function (response) {
                         console.log(response);
                    }
                });
            }
        }

How to populate to AutoComplete control ? 

Regards, 
Hendi 


1 Reply

BC Berly Christopher Syncfusion Team September 2, 2019 12:32 PM UTC

Hi Hendi, 
  
Greetings from Syncfusion support.  
  
We would like to inform you that we have provided the Web API Adaptor support for AutoComplete component instead of AJAX call as mentioned in the below code example. 
  
[index.cshtml] 
<h4>Auto Complete with Web API Adaptor using API</h4> 
        <ejs-autocomplete id="games" placeholder="Select a Game"> 
            <e-data-manager url="/api/Default/" adaptor="WebApiAdaptor"></e-data-manager> 
        </ejs-autocomplete> 
 
  
Create the API controller and paste the below content to achieve your requirement. 
  
[DefaultController.cs] 
public class DefaultController : ControllerBase 
    { 
        List<string> game = new List<string>(); 
        // GET: api/Default 
        [HttpGet] 
        public List<string> Get() 
        { 
                game.Add("Badminton"); 
                game.Add("Basketball"); 
                game.Add("Cricket"); 
                game.Add("Golf"); 
                game.Add("Gymnastics"); 
                game.Add("Tennis"); 
                game.Add("Hockey"); 
                return game; 
        } 
 
  
Else, if you want to set the data source with AJAX call, we suggest you to use the below mentioned code example to achieve your requirement in the created event of the AutoComplete component.  
  
[index.cshtml] 
   <h4>Auto Complete with Web API Adaptor using Ajax call</h4> 
        <ejs-autocomplete id="autoComplete" created="onCreate" placeholder="Employee Name" autofill="true" noRecordsTemplate="@Html.Raw("<span class='norecord'> NO DATA AVAILABLE</span>")"> 
        </ejs-autocomplete> 
<script> 
 
       function onCreate(e) {  
        var autoObj = document.getElementById('autoComplete').ej2_instances[0];  
        $.ajax({  
            type: "POST",  
            url: "/Home/FFUploadSiteselected",  
            data: JSON.stringify({ ssite: autoObj.value }),  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function (data) { 
                autoObj.dataSource = data;  
                autoObj.dataBind();  
            },  
            error: function (response) {  
                alert("Error");  
            }  
        });  
    }  
</script> 
 
  
[HomeController.cs] 
public class HomeController : Controller 
        { 
        List<string> emp = new List<string>(); 
 
        public object FFUploadSiteselected(string ssite) 
        { 
            emp.Add("Andrew Fuller"); 
            emp.Add("Nancy Devolio"); 
            emp.Add("Laura Callahan"); 
            return Json(emp); 
        } 
 
 
  
Please find the sample link from below. 
  
To know more about data binding, please refer the below UG and Demo link. 
  
  
Still facing issues, please revert us with issue reproducing sample that will help us to check and provide the exact solution at our end.  
 
  
Regards, 
Berly B.C 


Loader.
Live Chat Icon For mobile
Up arrow icon