MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the action and a jquery/javascript function to call the controller method. Note: we will use Nortwind database. Start by writing a controller method say CustomerAutoComplete which will return us list of customer Id and contact name in json format.

public ActionResult CustomerAutoComplete(string searchText)
    NWDataContext db = new NWDataContext();
    var customers = db.Customers.Where(x => x.ContactName.StartsWith(searchText))
            .Select(p => new { p.CustomerID, p.ContactName });

     // Return into json format
     return Json(customers, JsonRequestBehavior.AllowGet);

We used Linq to Sql for ease, this is what we need to fetch data from database and return result in json format to the page.

Time to adjust our HTML/rezor to type and get selected customer

<div>Customer Contact</div>
    <input type="text" id="customerContact" value=""/>
    <input type="hidden"   id="customerID"  name="customerID" />

Note we need to use two jquery js files

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" 
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" 

Whatever we need for autocomete, we already done, so let's check out put, whether Id and text which we selected are set or not.

Here is HTML, when we open the page

    <input id="customerContact" class="ui-autocomplete-input" type="text" 
    <input id="customerID" type="hidden" name="customerID">

Now type something in textbox and select a contact name so the output will be, i type car

  <input id="customerContact" class="ui-autocomplete-input" 
  <input id="customerID" type="hidden" name="customerID" value="FRANR">

If you can see hidden field value ="FRANR" (CustomerID)

Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • mvc
  • jquery
  • json
  • auto-complete
By Ali Adravi On 29 Jun, 13  Viewed: 7,022

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 40,241

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 106,061

How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden   On 30 Jun 2013  Viewed: 3,771

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 25,571

ASP.Net MVC file upload

How to upload file or image in mvc is not difficult but is totally different compare to FileUpload control. If we are developer and we are new to mvc then we immediately noticed we don’t have server side controls so the FileUpload control as well and our problem start here.... By Ali Adravi   On 25 May 2013  Viewed: 8,088