C# 页面中未显示多个相关表的数据
我正在使用.NETCore2.2。运行时,FacultyInterestItem列表中显示以下输出,其中缺少Faculty或Keywords表数据。虽然学院和关键字类与学院实体相关 以下是剃须刀页面C# 页面中未显示多个相关表的数据,c#,asp.net-core,razor-pages,asp.net-core-2.2,ef-core-2.2,C#,Asp.net Core,Razor Pages,Asp.net Core 2.2,Ef Core 2.2,我正在使用.NETCore2.2。运行时,FacultyInterestItem列表中显示以下输出,其中缺少Faculty或Keywords表数据。虽然学院和关键字类与学院实体相关 以下是剃须刀页面 <tbody> @foreach (var item in Model.FacultyInterestItems) { <tr> <td> @Html.DisplayFor(modelItem => item.Id
<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>
关键字表:
未从数据库中获取教员和关键字的数据。请告诉我解决方案尝试调用Include方法
我这样做了,它在这一行facultyInterestItem=JsonConvert.DeserializeObjectresult上给出了错误;JsonSerializationException:反序列化数组时意外结束。路径“[0]。faculty.FacultyInterestims”,第1行,207号位置。已解决!谢谢Martin,问题在于我生成的模型,我在Faculties.cs中的公共虚拟ICollection FacultyInterestItems{get;set;}中删除了它
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;
}
[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; }
}
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; }
}
_context.FacultyInterestItems.Include(x => x.Faculty).Include(x => x.Keyword).ToListAsync()