0

I have a auto complete box set up. however when a user types in the box a list of every single item appears. Not relating to anything the user is typing in.

What am i doing wrong?

Jquery:

 var availableTags = '@Url.Action("PopSearch", "Home")';
    $("#searchtxt").autocomplete({
        source: availableTags
    });

Controller function:

public ActionResult PopSearch()
    {
        IndustryManager manager = new IndustryManager();
        ProductRangeManager manager2 = new ProductRangeManager();
        ProductCategoryManager manager3 = new ProductCategoryManager();

        IList<Industry> industryList = manager.GetIndustries();
        IList<ProductRange> rangeList = manager2.GetAllProductRanges();
        IList<ProductCategory> categoryList = manager3.GetAllProductCategories();

        var attributes = industryList.Select(x => x.Name)
            .Union(rangeList.Select(x => x.Name))
             .Union(categoryList.Select(x => x.Name)).ToArray();

        return Json(attributes, JsonRequestBehavior.AllowGet);
    }

1 Answer 1

1

Check the server request. Probably you have a request to the server something like

http://yoursite.com/Home/PopSearch?term=Something

Where something is what user inputs in the textbox. It means that you need to filter it on the server side and return already filtered data.

public ActionResult PopSearch(string term)
{
    IndustryManager manager = new IndustryManager();
    ProductRangeManager manager2 = new ProductRangeManager();
    ProductCategoryManager manager3 = new ProductCategoryManager();

    IList<Industry> industryList = manager.GetIndustries();
    IList<ProductRange> rangeList = manager2.GetAllProductRanges();
    IList<ProductCategory> categoryList = manager3.GetAllProductCategories();

    var attributes = industryList.Select(x => x.Name)
        .Union(rangeList.Select(x => x.Name))
         .Union(categoryList.Select(x => x.Name)).ToArray();

    var result = attributes.Where(x => x.ToLower().StartWith(term.ToLower()));

    return Json(result, JsonRequestBehavior.AllowGet);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sure it doesn't. I just built link whitch you should expect on every ajax call from autocomplete to your server.
Actually you don't need to change anything in your javascript. you need to modify your action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.