当使用 fluent api 包含相关数据时,列表未填充在 ajax 中(使用 Web Api)

when the related data is included using fluent api, the list not populated in ajax (using Web Api)

在我的 ASP.NET Web Forms 应用程序(使用代码优先的 EF 和 Web Api)中,我需要使用 [=40] 读取项目列表 (List<Post>) =] 并使用以下代码填充帖子列表。但是,我遇到了一个奇怪的问题,详见下文。

 function LoadPostsByTimeframe(currTimeframeId) {
        jQuery.support.cors = true;
        $.ajax({
            url: '/api/post/GetPostsByTimeframe?tfId=' + currTimeframeId,
            type: 'GET',
            contentType: "application/json; charset=utf-8;",
            dataType: 'json',
            success: function (response) {
                var posts = response.d;
                //do stuff
            },
            error: function (x, y, z) {
                alert(x + '\n' + y + '\n' + z);
            }
        });
    }

在我的控制器 class 中,当我使用以下方法检索项目列表时:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public IList<Post> GetPostsByTimeframe(int tfId)
    {
        gEchoLuDBContext db = new gEchoLuDBContext();
        var posts = db.Posts.Where(p=>p.TimeFrameId == tfId).ToList();

        return posts;           
    }

我得到了每件物品(第三件不适合屏幕)

但是,我需要检索每个项目的相关人员数据。因此,我需要使用以下代码(Include)来检索项目。

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public IList<Post> GetPostsByTimeframe(int tfId)
    {
        gEchoLuDBContext db = new gEchoLuDBContext();
        var posts = db.Posts.Include(po=>po.Person).Where(p=>p.TimeFrameId == tfId).ToList();

        return posts;           
    }

我有这个输出(只返回第一项,其他两项为空):

在这两种情况下(包含或不包含),控制器方法 returns 一个有效的 List<Post> 包含所有项目。但是,对于包含版本,列表值(第一个值除外)不会显示在 ajax/jquery 侧。

你觉得我遗漏了什么吗?

尝试在 .ToList() 之前添加 .AsNoTracking()