c# - Razor 页面中未显示多个相关表的数据
问题描述
我正在使用 .net 核心 2.2。当我运行以下输出时,FacultyInterestItem列表中显示了Faculty或Keywords表数据缺失。虽然 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; }
}
教师和关键字的数据不是从数据库中获取的。请让我知道解决方案
解决方案
尝试调用Include
方法
_context.FacultyInterestItems.Include(x => x.Faculty).Include(x => x.Keyword).ToListAsync()
推荐阅读
- java - 如何通过 Gremlin 查询具有 OR 关系的多条边
- java - 从 Java 中的 AWS 参数存储中获取值
- r - 在 bookdown 菜单中未正确生成链接
- python - Python mayavi:如何在 3D 空间中的随机位置绘制球体
- python - 根据条件过滤 posgres 表
- android - 单元测试在调用演示者函数后出现 NullPointerException
- java - 字符串到字节的转换并再次返回不返回相同的结果 (ASCII)
- c - 打印某个结构会产生意外的输出
- ios - 如果我将应用程序移到后台然后在 iOS 中转到前台,则呈现的视图控制器会被解除
- docker - 多阶段构建找不到文件