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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

AutoComplete With WebApi

Thread ID:

Created:

Updated:

Platform:

Replies:

147046 Aug 30,2019 04:00 AM UTC Sep 2,2019 12:32 PM UTC ASP.NET Core - EJ 2 1
loading
Tags: AutoComplete
Hendi Lee
Asked On August 30, 2019 04:00 AM UTC

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 


Berly Christopher [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon