我面臨一個麻煩,試圖找出如何使用這個autocomplete plugin,並使其消耗我的WCF服務的方法。 WCF正確發佈和工作。
什麼可以做到自動完成插件的工作,還是有另一種聰明的方式使WCF的自動完成工作,並獲得選擇Person.Id和Person.Name?
下面有些事情我已經工作到現在:
WCF服務
public class Person {
public int Id {get;set;}
public string Name {get;set;}
public static List<Person> List(){
/*long query supressed to no be annoying :) */
}
}
[ServiceContract]
public interface IChatService
{
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "GetPeople",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
List<Person> GetPeople();
}
public class MyService : IMyService
{
public List<Person> GetPeople()
{
return Person.List();
}
}
現在aspx頁面:
....
<head>
<script type="text/javascript" src="http://view.jquery.com/trunk/plugins/autocomplete/lib/jquery.js"></script>
<script type='text/javascript' src='http://view.jquery.com/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js'></script>
<script type='text/javascript' src='http://view.jquery.com/trunk/plugins/autocomplete/lib/jquery.ajaxQueue.js'></script>
<script type='text/javascript' src='http://view.jquery.com/trunk/plugins/autocomplete/lib/thickbox-compressed.js'></script>
<script type='text/javascript' src='http://view.jquery.com/trunk/plugins/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="http://view.jquery.com/trunk/plugins/jquery.autocomplete.css" />
<link rel="stylesheet" type="text/css" href="http://view.jquery.com/trunk/plugins/lib/thickbox.css" />
<script>
$().ready(function() {
$('#<%=peopleNames.ClientID%>').autocomplete("http://localhost/MyService/MyService.svc/GetPeople", {
width: 260,
selectFirst: false
});
$("#<%=peopleNames.ClientID%>").result(function (event, data, formatted) {
alert('ok');
if (data) {
alert($(this).parent().next().find("input").val(data[1]));
}
});
});
</script>
</head>
<body>
<form runat="server">
<asp:TextBox ID="peopleNames" runat="server" MaxLength="500"></asp:TextBox>
</form>
</body>
</html>
只是用於測試目的,想法是讓Web用戶鍵入一個名稱,並且jQuery將調用WCF服務http://localhost/MyService/GetPeople列出SQL Server數據庫中的所有人員(將來的GetPeople方法將有一個字符串參數)。
jquery 自動完成插件似乎很好,但我不確定哪些js文件需要在我的本地計算機上工作。現在我不能使它工作或調試它,即使它顯示alert();
那麼Person Class中的[DataConctract] [DataMember]屬性呢? – mmutilva 2010-11-06 18:42:22
完成。我看到WCF運行良好。我正在看http://jqueryui.com/demos/autocomplete/#remote看看發生了什麼 – 2010-11-06 19:07:03