2013-10-28 140 views
0

尝试使用JQueryUI实现对Windows用户帐户的简单搜索。jQueryUI自动完成不会将结果呈现给输入框

要求用户输入名字或姓氏的HTML<input>控制和应该返回全名的所有可能的匹配(用户名),该搜索项。虽然服务器返回的结果如下:

enter image description here

问题:本<input>框显示搜索词,显示不带选项“白”下拉列表。 enter image description here

JQuery的代码:

     $(document).ready(function() { 
         $("#nameSearch").autocomplete({ 
          source: function (request, response) { 
           $.ajax({ 
            type: "POST", 
            contentType: "application/json; charset=utf-8", 
            url: "Search.aspx/GetUserDetails", 
            data: "{'username':'" + request.term + "'}", 
            dataType: "json", 
            async: true, 
            success: function (data) { 
             response($.map(data, function (item) { 
              return { 
               value: item.username 
              } 
             })); 

            }, 
            error: function (xhr, textStatus, errorThrown) { 
             var errorMessage = "Ajax error: " + this.url + " textStatus: " + textStatus + " errorThrown: " + errorThrown + " xhr.statusText: " + xhr.statusText + " xhr.status: " + xhr.status; 
             alert(errorMessage); 
             if (xhr.status != "0" || errorThrown != "abort") { 
              alert(xhr.responseText); 
             } 
            } 
           }); 
          } 
         }); 
        }); 

代码背后

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static Person[] GetUserDetails(string username) 
    { 
     List<Person> allUsers = new List<Person>(); 

     PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "abcd", 
     "dc=abcdH,dc=com"); 

     UserPrincipal qbeUser = new UserPrincipal(ctx); 

     qbeUser.GivenName = username; 

     PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 
     foreach (var found in srch.FindAll()) 
     { 
      Person user = new Person(); 
      user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")"; 
      allUsers.Add(user); 
     } 
     qbeUser = null; 
     qbeUser = new UserPrincipal(ctx); 

     qbeUser.Surname = username; 

     PrincipalSearcher srch1 = new PrincipalSearcher(qbeUser); 
     foreach (var found in srch1.FindAll()) 
     { 
      Person user = new Person(); 
      user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")"; 
      allUsers.Add(user); 
     } 
     qbeUser = null; 
     qbeUser = new UserPrincipal(ctx); 

     qbeUser.SamAccountName = username; 

     PrincipalSearcher srch2 = new PrincipalSearcher(qbeUser); 
     foreach (var found in srch2.FindAll()) 
     { 
      Person user = new Person(); 
      user.userDetails = found.DisplayName + " (" + found.SamAccountName + ")"; 
      allUsers.Add(user); 
     } 

     //allUsers.Sort(); 
     return allUsers.ToArray(); 


    } 
    public class Person 
    { 
     public string userDetails { get; set; } 
    } 

我必须在这里做得不对,我不能通俗易懂发现。尝试了很多不同的答案,但不符合我的问题。

回答

1

您在返回Person[]和你的成功功能,您要使用item.username并从Person定义它没有与username

任何财产可以哟你试试item.userDetails,看看是否显示你的结果。

1

我不知道这是否会申请,但在MVC4我已经使用自动完成在控制器和我回路线

return Json(items, JsonRequestBehavior.AllowGet); 

的项目是列表和返回类型为JsonResult

+0

对不起,答案没有帮助。 – shaz