JQuery UI自动完成和通用处理程序(ashx) - C#ASP.NET

Jas*_*son 1 c# jquery autocomplete httphandler

我正在尝试使用JQuery Autocomplete,但我想我无法从我的处理程序中获取它所期望的格式.

这是处理程序的作用.这是另一个SO问题....

 context.Response.ContentType = "text/plain";
 var companies = GetCompanies(); //This returns a list of companies (List<string>)

 foreach (var comp in companies)
 {
     context.Response.Write(comp + Environment.NewLine);
 }
Run Code Online (Sandbox Code Playgroud)

这不起作用.它肯定会被调用,它会返回我希望此代码返回的内容.有任何想法吗?

Bro*_*ass 6

它确实需要采用JSON格式,这里是我之前使用的一般大纲的示例:

    class AutoCompleteEntry
    {
        public int id { get; set; }
        public string label { get; set; }
        public string value { get; set; }
    }

    private void GetAutoCompleteTerms()
    {
        Response.Clear();
        Response.ContentType = "application/json";

        //evaluate input parameters of jquery request here

         List<AutoCompleteEntry> autoCompleteList= new List<AutoCompleteEntry>();
        //populate List of AutocompleteEntry here accordingly

        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        string json = jsSerializer.Serialize(autoCompleteList);
        Response.Write(json);
        Response.End();
    }
Run Code Online (Sandbox Code Playgroud)