2015-05-25 86 views
0

當我執行一個Ajax get請求時,服務器總是返回null爲導航屬性。導航屬性null與ajax

我的導航屬性是element.Escuderia。

我的代碼:

$.ajax({ 
     type: "GET", 
     url: "/GestionPilotosEscuderia/GetPiloto", 
    }).done(function (data) { 
     $(data).each(function (index, element) { 
      console.log(element) //here element.Escuderia gives null 
     }); 
    }).error(function (ex) { 
     alert("Error"); 
    }); 
//Controller 
public JsonResult GetPiloto() 
     { 
      db.Configuration.ProxyCreationEnabled = false; 
      return Json(db.Piloto.ToList(), JsonRequestBehavior.AllowGet); 
     } 

public partial class Piloto 
    { 
     public Piloto() 
     { 
      this.PilotoCarrera = new HashSet<PilotoCarrera>(); 
     } 

     public int id { get; set; } 
     public int? id_escuderia { get; set; } 
     public string nombre { get; set; } 
     public string apellidos { get; set; } 

     public virtual Escuderia Escuderia { get; set; } 

    } 
+0

您可以顯示JSON內部/ GestionPilotosEscuderia/GetPiloto –

回答

0

我認爲,導航性能,你需要包括它們。

return Json(db.Piloto**.Include("Escuderia").**ToList(), JsonRequestBehavior.AllowGet); 
0

這給了我一個循環引用錯誤,如果db.piloto.tolist()會在周圍的觀點,如果我訪問的導航屬性(「Escuderia」),但如果我用AJAX得到無法訪問物業導航

0

問題解決 刪除: db.Configuration.ProxyCreationEnabled = false;

0

你可以試試這個javascript代碼:

$.ajax({ 
    method: "GET", 
    url: "/GestionPilotosEscuderia/GetPiloto", 
    contentType: "application/json; charset=utf-8", 
    success: function(data){ 
    //you can use $.each to loop on the data. 
    console.log(data) 
    }, 
    error: function(error){ 
    alert(error); 
    } 
});