I'm trying to Serialize a list of objects to Json and I have received the following error:
An exception of type
'System.InvalidOperationException' occurred in Microsoft.Web.Extensions.dll but was not handled in user code Additional information: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Pais_FE2360F9C3DD9892003FC5DC33EF9BD07698CFF62033EE442867F74C88F09AC7'.
I have used:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetListaPais(string prefixText, string contextKey)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(AuxiliarControler.ListaPais(prefixText,contextKey));
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Flush();
Context.Response.Write(json);
}
Method that gets the list of Pais
:
public static List<Pais> ListaPais(string nomePais, int IDIdioma)
{
using (entidadesIUS entidades = new entidadesIUS())
{
return entidades.Pais.Where(p => p.IDIdioma == IDIdioma &&
p.Pais1.Contains(nomePais)).ToList();
}
}
Entity Pais
public partial class Pais
{
public Pais()
{
this.CAL = new HashSet<CAL>();
this.Clientes = new HashSet<Clientes>();
}
public int IDPais { get; set; }
public string Pais1 { get; set; }
public int IDIdioma { get; set; }
public virtual ICollection<CAL> CAL {get; set; }
public virtual Idiomas Idiomas {get; set; }
public virtual ICollection<Clientes> Clientes {get; set; }
}