In this article I will explain with an example, how to populate jQuery AutoComplete form API in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
ASPSnippets Test API
The following API will be used in this article.
The API returns the following JSON.
    "CustomerId": 1,
    "Name""John Hammond",
    "Country""United States"
    "CustomerId": 2,
    "Name""Mudassar Khan",
    "CustomerId": 3,
    "Name""Suzanne Mathews",
    "CustomerId": 4,
    "Name""Robert Schidner",
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method gets called when the Form is submitted.
Inside this Action Method, the Name and CustomerId of the selected customer are fetched and set into a ViewBag object.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View();
    public ActionResult Index(string name, int customerId)
        ViewBag.Message = "Name: " + name + "\\nID: " + customerId;
        return View();
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The View also consists of:
TextBox – For capturing Name of the customer.
HiddenField – For capturing selected CustomerId from the AutoComplete.
Button – For submitting the Form.
jQuery AutoComplete Plugin implementation
Inside the View, the following jQuery UI CSS file is inherited.
1. jquery-ui.css
And then, the following jQuery and jQuery UI JS files are inherited.
1. jquery.min.js
2. jquery-ui.min.js
Inside the jQuery document ready event handler, the TextBox has been applied with the jQuery AutoComplete plugin.
A jQuery Get call is made to get list of customers returned from the URL which acts as a source of data to the jQuery AutoComplete.
Then, a Select event handler has been defined for the jQuery AutoComplete and items are selected from the AutoComplete List.
When the customer Name is selected and the Submit button is clicked, the Form is submitted and the ViewBag object is checked for NULL and if it is not NULL then, the value of the ViewBag object is displayed using JavaScript Alert Message Box.
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="" />
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
                source: function (request, response) {
                    var prefix = request.term.toLowerCase();
                    $.get("", function (data, status) {
                        var customers = JSON.parse(data);
                        if (prefix != '') {
                            customers = customers.filter(function (customer) {
                                return customer.Name.toLowerCase().indexOf(prefix) != -1
                        response($.map(customers, function (customer) {
                            return {
                                label: customer.Name,
                                val: customer.CustomerId
                select: function (e, i) {
                minLength: 0
            }).focus(function () {
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        <input type="text" id="txtName" name="name" />
        <input type="submit" value="Submit" />
        <input type="hidden" id="hfCustomerId" name="customerId" />
    @if (ViewBag.Message != null)
        <script type="text/javascript">
            window.onload = function () {
Populate jQuery AutoComplete from API in ASP.Net MVC