Introduction
This articles describes the Auto Suggested TextBox in the Web API. Type a letter in the TextBox and press the Enter key. It then displays an Autosuggest list.
The following is the procedure for creating the application.
Step 1
Create a Web API application.
- Start Visual Studio 2012.
- From the start window Select "Installed" -> "Visual C#" -> "Web".
- Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

- From the "MVC4 Project" window select "Web API".

- Click on the "OK" button.
Step 2
Now select the "HomeController".
- In the "Solution Explorer".
- Expand the Controller folder.
- Select the "HomeController".

Add the following code:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- namespace AutosuggestTextAPI.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public DataTable Items()
- {
- DataTable cityname = new DataTable();
- cityname.Columns.Add("ID", typeof(int));
- cityname.Columns.Add("Level", typeof(string));
- cityname.Columns.Add("value", typeof(string));
- cityname.Columns.Add("word", typeof(string));
- DataRow dr = cityname.NewRow();
- dr["ID"] = 1;
- dr["Level"] = "2";
- dr["value"] = "Kanpur";
- dr["word"] = "KNP";
- cityname.Rows.Add(dr);
- dr = cityname.NewRow();
- dr["ID"] = 2;
- dr["Level"] = "3";
- dr["value"] = "Lucknow";
- dr["word"] = "LKW";
- cityname.Rows.Add(dr);
- dr = cityname.NewRow();
- dr["ID"] = 3;
- dr["Level"] = "4";
- dr["value"] = "Delhi";
- dr["word"] = "DLH";
- cityname.Rows.Add(dr);
- dr = cityname.NewRow();
- dr["ID"] = 3;
- dr["Level"] = "5";
- dr["value"] = "Banaras";
- dr["word"] = "BNA";
- cityname.Rows.Add(dr);
- dr = cityname.NewRow();
- dr["ID"] = 4;
- dr["Level"] = "6";
- dr["value"] = "Banglore";
- dr["word"] = "BNG";
- cityname.Rows.Add(dr);
- return cityname;
- }
- public ActionResult CityAutoSuggestByName(string autotext)
- {
- autotext = (autotext == null) ? "" : autotext;
- return Content(JsonForAutoSuggestforName(Items().Select("value LIKE '%" + autotext + "%'")), "application/json");
- }
- static string loc;
- public static string JsonForAutoSuggestforName(DataRow[] datarow)
- {
- StringBuilder jsonBuilder = new StringBuilder();
- jsonBuilder.Append("[");
- for (int a = 0; a < datarow.Length; a++)
- {
- jsonBuilder.Append("{\"ID\": \"");
- jsonBuilder.Append(datarow[a][0].ToString());
- jsonBuilder.Append("\",");
- jsonBuilder.Append("\"Level\": \"");
- loc = datarow[a][2].ToString().Replace("\\", "\\\\");
- jsonBuilder.Append(loc.Replace("\"", "\\\""));
- jsonBuilder.Append("\",");
- jsonBuilder.Append("\"value\": \"");
- datarow[a][2].ToString().Replace("\\", "\\\\");
- jsonBuilder.Append(loc.Replace("\"", "\\\""));
- jsonBuilder.Append("\",");
- jsonBuilder.Append("\"word\": \"");
- loc = datarow[a][3].ToString().Replace("\\", "\\\\");
- jsonBuilder.Append(loc.Replace("\"", "\\\""));
- jsonBuilder.Append("\",");
- jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
- jsonBuilder.Append("},");
- }
- jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
- jsonBuilder.Append("]");
- string jason = jsonBuilder.ToString();
- return jsonBuilder.ToString();
- }
- }
- }




Comments
Join the conversation! Your thoughts help the community grow.