- Home
- Forum
- ASP.NET Core - EJ 2
- AutoComplete With WebApi
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
SIGN IN To post a reply.
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.
Sample Link: https://www.syncfusion.com/downloads/support/forum/147046/ze/EJ2_Core_147046_WebAPI1297207594
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
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
HL Hendi Lee
- Aug 30, 2019 04:00 AM UTC
- Sep 2, 2019 12:32 PM UTC