[英]How can I get field from second table via first table with foreign key from javascript?
我有兩個簡單的模型:書:
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的方法,我可以從表格中獲取所有數據。 如何使用外鍵GenreId從表Genres中獲取類型的JavaScript代碼? 我想寫類似book.Genre.Name的東西,但是在js中不起作用
您可以嘗試以下代碼來序列化對象,以使用json格式返回前端:
var genre = new Genre();
JavaScriptSerializer js = new JavaScriptSerializer();
string data = js.Serialize(Genre);
或者以其他方式,您可以使用Json.NET做到這一點,這是一個功能強大的json對象轉換庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.