Requirements
- Visual Studio 2010 and high.
- SQL Server 2008 and high.
Knowledge required
- Basic knowledge of jQuery AJAX.
- Basic Knowledge of WebAPI.
- Basic Knowledge of Kendo UI.
- Basic knowledge of Entity Framework.
Article flows
- Create a database and insert values (DB).
- Create Web-API project and configure Entity Framework(WEB-API).
- Write Services to get the data(WEB-API).
- Create MVC Application(ASP.NET MVC).
- Create Controller and View(ASP.NET MVC).
- Write AJAX to call Web-API and bind the data to Kendo UI autocomplete (jQuery AJAX)
Step 1
DB
Create a table.
Insert the values.
Step 2
Web-API
Create Web-API project and configure Entity Framework.
Refer to the link given below to configure Entity Framework.
http://www.c-sharpcorner.com/blogs/implement-database-first-approach-with-entity-framework after configuring our Entity Framework with Web-API will be, as shown below.
Now, write the Services to get the data. Here, I have used the repository pattern to get the data to know about repository pattern, refer to the
links.
-
-
-
-
-
-
-
- [ResponseType(typeof(IEnumerable < Employee > ))]
- [System.Web.Http.Route("api/GetEmployees")]
- [System.Web.Http.HttpGet]
- public HttpResponseMessage GetEmployees()
- {
- var result = _Employeerepository.GetAll();
- return GetResultResponse(result);
- }
Step 3
ASP.NET MVC
Create MVC Application after the creation of an Application. Create control in the name of Crud and create actionmethod in the name of AutoComplete.
Action Method
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace TS_VMS.Controllers {
- public class CrudController: Controller {
- public ActionResult AutoComplete() {
- return View();
- }
- }
- }
Addview
Right click action method and click addview.
Create a view without modal with the name AutoComplete.
Add CSS and Script
Add CSS given below and the script to access the predefind function from Kendo UI.
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
Add Textbox control in view.
- @Html.TextBox("Designation", "", new { @id = "Designation", @class = "form-control", @placeholder = "Designation" })})
Add AJAX call to call Web-API Service and bind the data to the textbox
- <script type="text/javascript">
- $(document).ready(function() {
- var ApiUrl = "http://localhost:50537/";
- $.ajax({
- url: ApiUrl + "api/GetEmployees",
- type: "Get",
- dataType: 'json',
- contentType: 'application/json; charset=utf-8',
- success: function(result) {
- $("#Designation").kendoAutoComplete({
- clearButton: true,
- dataSource: result,
- filter: "startswith",
- dataTextField: "Designation",
- minLength: 1,
- placeholder: "Enter Designation ...",
- });
- }
- });
- });
- </script>
Description(in above AJAX call)
- URL - It represents Web-API URL with the name of API/GetEmployees, refer Web-API get method.
- clearButton - true. It's to clear that the textbox value is not a binded value.
- filter - "startswith". There are many filter options to filter respective field, which is based on the input like starting value contains an ending number.
- dataTextField - "Designation". Mention which field(value) you want to display to the user.
- minLength - 1. Tto start searching the string until reaching the minlenth of our input. It's not involved to search from that Data Source.
Finally, the view should be, as shown below.
- <!DOCTYPE html>
- <script type="text/javascript">
- $(document).ready(function() {
- var ApiUrl = "http://localhost:50537/";
- $.ajax({
- url: ApiUrl + "api/GetEmployees",
- type: "Get",
- dataType: 'json',
- contentType: 'application/json; charset=utf-8',
- success: function(result) {
- $("#Designation").kendoAutoComplete({
- clearButton: true,
- dataSource: result,
- filter: "startswith",
- dataTextField: "Designation",
- minLength: 1,
- placeholder: "Enter Designation ...",
- });
- }
- });
- });
- </script>
- <html>
-
- <head>
- <title>Kendo AutoComplete</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
- </head>
-
- <body> @Html.TextBox("Designation", "", new { @id = "Designation", @class = "form-control", @placeholder = "Designation" }) </body>
-
- </html>
Now, run our Application (it's searching in minimum length of 1 character itself).
Now, I am changing the minmum length to two.
minLength: 2,
Now, see the output (it's not populating the related data).
Now, type two characters.
I hope, you enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.