2016-11-15 55 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对象转换库。

相关问题