Autocomplete Textbox Using Jquery/JSON from Database

JSONIn web page designing  autocomplete property of textbox is widely used for making the site user friendly and interactive , so in this article we will understand the simple method for autocompletion of textbox using Jquery.

First we will see a simple example for this then in next step I will show you how you can fetch the data from the database using JSON



<!-- Load jQuery, jQuery UI and jQuery ui styles from jQuery website -->
<<link rel="stylesheet" href="" />
<script src=""></script>
<script src=""></script>

var data = [
function () {
source: data,
 autoFocus: true,


<label for="City">City <input type="text" name="CityNames" id="City" /></label>


After running the code in browser you will see the page having the city textbox which will be autocompleted when you start typing. AutoFillNow if you want your city name fetched from database  the simple way out for doing that will be using JSON data First write the method for getting the JSON data from database , I will show you a simple example for that ,

JSON Method –

public JsonResult AutoCompleteCityList(string term)
 JsonResult result = new JsonResult();
var vStateList = "write here your database connection code for gettig city name List ";
var namelist = vStateList.Where(n => n.CityName.ToLower().StartsWith(term.ToLower()));
 return Json(namelist, JsonRequestBehavior.AllowGet);


<script type="text/javascript">,


 $(document).ready(function () {



 source: function (request, response) {


 url: "/AutoCompleteCityList",

 type: "POST",

 dataType: "json",

data: { term: request.term },

 success: function (data) {

 response($.map(data, function (item) {

return {

label: item.CityId,

value: item.CityName




 select: function (event, ui) {




 focus: function (event, ui) {




messages: {

 noResults: "", results: "",








Just use this code and you will be easily able to use the autocomplete property in your  site .
Happy Coding!!


Leave a Comment

Your email address will not be published.