I have my webservice method like below:
<WebMethod()> _
Public Function TravelMeansList(ByVal prefix As String) As List(Of HRM_travellingMeans)
Dim ctx As New HRMEntities
Dim query = From c In ctx.HRM_travellingMeans Where c.name.ToUpper.StartsWith(prefix) Select c
If query.Count > 0 Then
Return query.ToList()
Else
Return Nothing
End If
End Function
and my script like below
<script type="text/javascript">
$(document).ready(function () {
$("#<%=travelmeansTextBox.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("../../services/ApplicationService.asmx/TravelMeansList") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response($.map(data.d, function (item) {
return Json({
value: item.name
})
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
},
minLength: 1
});
});
</script>
My problem is that anytime i try to type any letter in my textbox i get this error
A circular reference was detected while serializing an object of type \u0027System.Data.Metadata.Edm.AssociationType\u0027.”,”StackTrace”:” at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable ……
What am I doing wrong.
The problem may lie with the JSON serialization of the Entity Framework object. Refer to this article:
http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/
The author here suggests that you use a JSONResult object if you are using ASP.NET MVC, though I’m not sure how well that would play with jQuery Autocomplete.