我创建了两个班级" Candidat"和#34;经验"。候选人可能有很多经历
public class Consultant
{
public int ConsultantID { get; set; }
public string ConsultantNom { get; set; }
public string ConsultantPrenom { get; set; }
public string ConsultantTitre { get; set; }
public Disponibilite ConsultantDisponibilite { get; set; }
public virtual ICollection<Experience> Experiences { get; set; }
}
public class Experience
{
public int ExperienceID { get; set; }
public virtual Consultant Consultant { get; set; }
public int ConsultantID { get; set; }
public string ExperienceNomSociete { get; set; }
public string ExperiencePoste { get; set; }
public DateTime ExperienceDebut { get; set; }
public DateTime ExperienceFin { get; set; }
public string ExperienceCompetences { get; set; }
当我搜索所有候选人时,在我的控制器中,我写了这段代码
public JsonResult ConsultantsList()
{
var consultants = from c in _consultantRepository.getConsultants()
select c;
return Json(consultants, JsonRequestBehavior.AllowGet);
}
问题是当我提出一个断点时我收到了结果,但是在我的页面中有一个问题&#34;在序列化类型&#34的对象时检测到循环引用。
这就是我用JQuery解析数据的方法:
function PopulateConsultantList() {
$.ajax({
type: "GET",
url: "/Consultants/ConsultantsList",
success: function (data) {
console.log(data);
var json = $.parseJSON(JSON.stringify(data));
console.log(json);
var corpsTR = '';
$.each(json, function (i, item) {
corpsTR += '<tr><td>' + item.ConsultantPrenom + " " + item.ConsultantNom + '</td>'
+ '<td>' + item.ConsultantTitre + '</td>'
+ '<td>Immédiate</td>'
+ '<td><a href="/Consultants/Details/' + item.ConsultantID + '" class="btn btn-default btn-icon"><i class="fa fa-file-text-o"></i></a> <a href="/Consultants/Delete/' + item.ConsultantID + '" class="btn btn-danger btn-icon"><i class="fa fa-trash-o"></i></a></td></tr>';
});
$("tbody").append(corpsTR);
}
});
}
PS:当经验是空的时候,没有问题发出。
谢谢
答案 0 :(得分:1)
从Experience中删除Consultant属性,或者向其添加JsonIgnore属性。