首页 > 解决方案 > Razor 页面中未显示多个相关表的数据

问题描述

我正在使用 .net 核心 2.2。当我运行以下输出时,FacultyInterestItem列表中显示了FacultyKeywords表数据缺失。虽然 Faculties 和 Keywords 类与 FacltyInterestItems 相关联 缺少教师和关键字数据的教师兴趣项目列表

以下是剃须刀页面

<tbody>
    @foreach (var item in Model.FacultyInterestItems)
    {
        <tr>
 <td>     @Html.DisplayFor(modelItem => item.Id)           
        </td>                
 <td>
                @Html.DisplayFor(modelItem => item.Faculty.FacultyId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Keyword.Name)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
    }
</tbody>

Razor 页面的 cs 文件具有 OnGetAsync() 方法,它连接并从 WebAPI 获取数据

public async Task OnGetAsync()
{
    List<FacultyInterestItems> facultyInterestItem = new List<FacultyInterestItems>();
    HttpClient client = _api.Initial();
    HttpResponseMessage res = await client.GetAsync("api/FacultyInterestItems");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        facultyInterestItem = JsonConvert.DeserializeObject<List<FacultyInterestItems>>(result);
    }
    List<Faculties> listOfFaculty = new List<Faculties>();
    res = await client.GetAsync("api/Faculties");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        listOfFaculty = JsonConvert.DeserializeObject<List<Faculties>>(result);
    }
    List<Keywords> listOfKeywords = new List<Keywords>();
    res = await client.GetAsync("api/Keywords");
    if (res.IsSuccessStatusCode)
    {
        var result = res.Content.ReadAsStringAsync().Result;
        listOfKeywords = JsonConvert.DeserializeObject<List<Keywords>>(result);
    }
    FacultyInterestItems = facultyInterestItem;
    Keywords = listOfKeywords;
    Faculties = listOfFaculty;

}

razor page cs 文件中的过程OnGetAscyn()从 API 获取数据。这是连接到数据库并获取数据的 API 控制器中的 get 方法。

[HttpGet]
public async Task<ActionResult<IEnumerable<FacultyInterestItems>>> GetFacultyInterestItems()
{
    return await _context.FacultyInterestItems.ToListAsync();
}

这是模型:

院系表

public partial class Faculties
{
    public Faculties()
    {
        FacultyInterestItems = new HashSet<FacultyInterestItems>();
        SuggestedKeywords = new HashSet<SuggestedKeywords>();
    }

    public long Id { get; set; }
    public string FacultyId { get; set; }
    public DateTime? UpdateDate { get; set; }
    public DateTime? InsertDate { get; set; }

    public virtual ICollection<FacultyInterestItems> FacultyInterestItems { get; set; }
    public virtual ICollection<SuggestedKeywords> SuggestedKeywords { get; set; }
}

FacultyInterestItems 表:

public partial class FacultyInterestItems
{
    public long Id { get; set; }
    public long? FacultyId { get; set; }
    public int? KeywordId { get; set; }
    public DateTime InsertDate { get; set; }
    public DateTime UpdateDate { get; set; }

    public virtual Faculties Faculty { get; set; }
    public virtual Keywords Keyword { get; set; }
}

关键字表:

public partial class Keywords
{
    public Keywords()
    {
        FacultyInterestItems = new HashSet<FacultyInterestItems>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime InsertDate { get; set; }
    public DateTime UpdateDate { get; set; }
    public int? DepartmentId { get; set; }

    public virtual Departments Department { get; set; }
    public virtual ICollection<FacultyInterestItems> FacultyInterestItems { get; set; }
}

教师和关键字的数据不是从数据库中获取的。请让我知道解决方案

标签: c#asp.net-corerazor-pagesasp.net-core-2.2ef-core-2.2

解决方案


尝试调用Include方法

 _context.FacultyInterestItems.Include(x => x.Faculty).Include(x => x.Keyword).ToListAsync()

推荐阅读