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 8652031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:12:47+00:00 2026-06-12T14:12:47+00:00

I am using a WebApi controller to return an IDictionary to jQuery autocomplete like

  • 0

I am using a WebApi controller to return an IDictionary to jQuery autocomplete like so:

public IDictionary<int, string> GetClientAuto(string term)
    {
        var clients = db.Clients.Where(n => n.Name.Contains(term)).OrderBy(n => n.Name);
        return clients.ToDictionary(n => n.ClientID, n => n.Name);
    }

The issue is although I add a breakpoint and check the variable clients is sorting by Name turns to to be true, the order shown in the autocomplete box is different, possibly I expect by the ID. I tried adding this to the autocomplete: sortResults:false, but with no effect.

I have this in my succes function, is there something here maybe I need to change for the order to work on the label i.e. Name:

success: function (json) {
                        // call autocomplete callback method with results
                        response($.map(json, function (name, val) {
                            return {
                                label: name,
                                value: val
                            }
                        }));
                    },
  • 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-12T14:12:48+00:00Added an answer on June 12, 2026 at 2:12 pm

    Its being sorted by the ToDictionary call, the order of dictionaries isn’t actually defined (http://msdn.microsoft.com/en-us/library/yt2fy5zk.aspx) as it shouldn’t matter, however i believe it is typically the value of the key, not the value.

    You could resort it in javascript to be by name, or you could return something other than a dictionary from your api. Either a IEnumerable> or IEnumerable would do the trick.

    Alterntively look into the OrderBy methods on the dictionary, however they all appear to return a list of KeyValuePairs.

    End result should look something like

    public IEnumerable<KeyValuePair<int, string>> GetClientAuto(string term)
    {
      return db.Clients.Where(n => n.Name.Contains(term)).OrderBy(n => n.Name).Select(n => new KeyValuePair<int, string>(n.ClientID, n.Name ));
    }
    
    success: function (json) 
    {
       response($.map(json, function (item) {
          return {
            label: item.Value,
            value: item.Key
          }
       }));
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use Sendgrid WebAPI preferably without SMTP or Swiftmailer using the code
I'm using MVC web Api for RESTful purpose. My controller methods return serialized Json
I have a controller which accepts public class MyModel { [MaxLength(400)] public string Message
I want to return some JSON data from my WebAPI controller and I want
using Newtonsoft.Json; namespace FAL.WebAPI2012.Controllers { public class Person { public int Id {get;set;} public
Using ASP.Net WebAPI, I have a controller with an asynchronous action similar to the
I am using MVC4's WEB API to expose a controller. Initially I created created
Using the ASP.NET Web-Api, I have the following POST setup in my controller. When
In my webapp, I am using Spring MVC as my controller layer. However, using
I am using mvc webapi to create a REST API and struggling to find

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.