Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:06:17+00:00 2026-06-04T21:06:17+00:00

I am implementing jQuery autocomplete in a text box and I am curious if

  • 0

I am implementing jQuery autocomplete in a text box and I am curious if my code looks right.

Here is my textbox from my view.

<div class="editor-field">
  @Html.TextBoxFor(model => model.Customer.CustomerName,  
                                      new {id = "CustByName" })
</div>

Here is the javascript to implement autocomplete for the textbox id.

$(document).ready(function () {
$("#CustByName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Cases/FindByName", type: "GET", dataType: "json",
            data: { searchText: request.term, maxResults: 10 },
            success: function (data) {
                response($.map(data, function (item) {
                    return { 
                       label: item.CustomerName, 
                       value: item.CustomerName, 
                       id: item.CustomerID }
                }))
            }
        })
    }
});

});

Here is the controller action called by the javascript:

public JsonResult FindByName(string searchText, int maxResults)
{
   CustomerFind find = new CustomerFind();
   var result = find.FindCustomerByName(searchText, maxResults);
   return Json(result);
}

Here is the function in CustomerFind called FindCustomerByName:

internal List<Models.Customer>  
               FindCustomerByName(string searchText, int maxResults)
{
        List<Models.Customer> cust = new List<Customer>();
        var result = from c in cust
                    where c.CustomerName.Contains(searchText)
                    orderby c.CustomerName 
                    select c;
        return result.Take(maxResults).ToList();
}

Here is what I have in my layout cshtml file for script reference.

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.20/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.20/jquery-ui.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/rls-functions.js")" type="text/javascript"></script>"

Everything seems to work ok, except the LINQ query in FindCustomerByName does not return any records even though they exist.

Can anyone suggest what might be the issue or suggest a better way to do autocomplete?

I have looked at numerous examples and cobbled this together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:06:18+00:00Added an answer on June 4, 2026 at 9:06 pm

    Second update. Found a couple syntax errors

    $(document).ready(function () {
        $("#CustByName").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Cases/FindByName", type: "GET", dataType: "json",
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { 
                               label: item.CustomerName, 
                               value: item.CustomerName, 
                               id: item.CustomerID }
                        })); // <--- semicolon here
                    }
                }); // <--- semicolon here
            },
            minLength: 3 // <-- sets the minimum number of characters to type before ajax fires
        });
    });
    

    Update based on OP not having a db context that he is getting data from

    Here is an example of making a query to the Database using EF. A database context has to be created first and then you use that context here in code.

    public IList<Lender> GetLenders(string partialText) {
        IList<Lender> lenders;
    
        // Create data connection to the LINQ to SQL class that represents the database
        using (DBDataContext dataContext = new DBDataContext()) {
    
            // Get all lenders where lender name contains partial text and put them into new Lender objects
            // The final call .ToList() puts all the Lender objects into a collection that can be enumerated
            lenders = (from data
                            in dataContext.LenderDBs
                        orderby data.LenderName
                        where data.IsActive == true
                            && data.LenderName.ToLower().Contains(partialText.ToLower())
                        select new Lender {
                            LenderName = data.LenderName,
                            URL = data.URL
                        }).Take(15).ToList();
        }
        return lenders;
    }
    

    Your TextBoxFor is missing the class that creates the Autocomplete feature class="ui-widget" but also make sure that you have this script in your layout for the styling of the dropdown

    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    
    @Html.TextBoxFor(model => model.Customer.CustomerName,  
                                      new {id = "CustByName", @class = "ui-widget" })
    

    And your action needs to allow for Json return by allowing the Get in the return

    public JsonResult FindByName(string searchText, int maxResults)
    {
       CustomerFind find = new CustomerFind();
       var result = find.FindCustomerByName(searchText, maxResults);
       return Json(result, JsonRequestBehavior.AllowGet);
    }
    

    Aside from all that the only other thing I see is contentType: "application/json; charset=utf-8" missing in your ajax request. Try putting an alert(data); just before the response statement in your success section. If that doesn’t hit then Ajax is erroring


    Also, as a separate concern, you are sending in maxResults to return your data, but your data is actually returning all rows where searchText matches and then once it gets to your client it is then taking the maxResults. I’d change that so that maxResults was part of your query so you aren’t returning more data than needed. Actually, I’d put maxResults as a configurable option in the web.config file. The UI doesn’t need to concern itself with rules like that.

    I’ve profiled using code like your orignal and code like below. If you watch SQL Server execution the server will return all rows with match with the code you had above. But the code below will create a Top 10 clause so that only 10 rows are returned

    internal List<Models.Customer>  FindCustomerByName(string searchText, int maxResults)
    {
            List<Models.Customer> cust = new List<Customer>();
            var result = (from c in cust
                        where c.CustomerName.Contains(searchText)
                        orderby c.CustomerName 
                        select c).Take(maxResults).ToList();
            return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing jQuery AutoComplete plugin like this: HTML: <input type='text' class='AutoComplete' url='/source/data'> jQuery:
I am implementing jQuery-ui autocomplete function in my website. In IE6, the autocomplete box
I am implementing the Jquery UI autocomplete. I have the following code. Application.js $(function()
I have an issue: I am implementing jQuery scroll bar ( from here )
I'm implementing the jQuery plugin niceScroll in my DIV's. It works beautifully except when
I am implementing a jquery autocomplete on a search form and am getting the
I was implementing a on-demand script controller based on jquery's getscript, it looks like
I am implementing the jQuery UI Autocomplete based on the categories example in the
I'm currently implementing jQuery UI's autocomplete in my clients webshop. The problem is: the
I'm implementing a Google Suggest like autocomplete feature for tag searching using jQuery's autocomplete.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.