2016-11-15 34 views
0

我有兩個簡單的模型: 書:如何通過第一個表與外部關鍵字從第二個表中獲取字段?

public class Book 
    { 
     public int BookID { get; set; } 
     public string Name { get; set; } 
     public string Author { get; set; } 
     public string Year { get; set; } 
     public string Publisher { get; set; } 

     public int GenreId { get; set; } 
     [ForeignKey("GenreId")] 
     public Genre Genre { get; set; } 
    } 

和體裁

public class Genre 
    { 
     public Genre() 
     { 
      Books = new List<Book>(); 
     } 
     public int GenreID { get; set; } 
     public string Name { get; set; } 

     public ICollection<Book> Books { get; set; } 
    } 

隨着法從ApiController我從表圖書的所有數據。 如何獲取javascript代碼使用外鍵GenreId的表格類型的名稱? 我想寫點東西像book.Genre.Name,但它不能在JS工作

+0

請向我們展示發送書籍的'Action'是如何返回的。它是否包含流派的細節?還包括執行請求的'js'。在問題中更新它,而不是評論 – Searching

回答

0

你可以試試下面的代碼序列化對象返回用JSON格式前端:

var genre = new Genre(); 
JavaScriptSerializer js = new JavaScriptSerializer(); 
string data = js.Serialize(Genre); 

或者換句話說,你可以使用Json.NET這樣做,這是一個功能強大的json對象轉換庫。

相關問題