Don't see object values from ASP.NET Core MVC in Angular 7 http get

半城伤御伤魂 提交于 2019-12-13 20:19:16

问题


I developed an ASP.NET Core 2.0 Web API service and try to use HttpGet method: http://localhost:50223/api/ArtistsLibrary/ListArtists

[HttpGet]
[Produces("application/json")]
[Route("ListArtists")]
public JsonResult ListArtists()
{
    var artists = _context.Artists.ToList();
    return Json(new { results = artists });
}

public class Artist
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    public FakeData fake { get; set; }
}

public class FakeData
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    public string name { get; set; } 
}

It returns:

{
    "results": [
        {
            "id": 1,
            "fake": {
                "id": 1,
                "name": "m1"
            }
        },
        {
            "id": 2,
            "fake": {
                "id": 2,
                "name": "m2"
            }
        }
    ] 
}

Everything is OK, but when I try to get the data from Angular 7, I got "fake = null":

getArtistsList(): Observable<Artist[]> {
    return this.http.get<Artist[]>(this.myAppUrl + 
          'api/ArtistsLibrary/ListArtists');   }

  LoadData() {
    this._artistsService.getArtistsList().subscribe((d) => {
      debugger;
      this.artists = d;
      console.log(this.artists);
    });
}

Results:

Array(2) 0: fake: null id: 1

1: fake: null id: 2

Please help me.

来源:https://stackoverflow.com/questions/53962597/dont-see-object-values-from-asp-net-core-mvc-in-angular-7-http-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!