多个相关表的数据未显示在 Razor 页面中
Data of Multiple related tables not shown in Razor page
我正在使用.net core 2.2。当我 运行 时,以下输出显示在 FacultyInterestItem 列表中,其中包含 Faculty 或 Keywords table 数据缺失。尽管 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 页面 cs 文件中的过程 OnGetAscyn(),从 API 获取数据。这是连接到数据库并获取数据的 API 控制器中的 get 方法。
[HttpGet]
public async Task<ActionResult<IEnumerable<FacultyInterestItems>>> GetFacultyInterestItems()
{
return await _context.FacultyInterestItems.ToListAsync();
}
这是模型:
五官Table
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 tables:
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; }
}
关键词table:
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()
我正在使用.net core 2.2。当我 运行 时,以下输出显示在 FacultyInterestItem 列表中,其中包含 Faculty 或 Keywords table 数据缺失。尽管 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 页面 cs 文件中的过程 OnGetAscyn(),从 API 获取数据。这是连接到数据库并获取数据的 API 控制器中的 get 方法。
[HttpGet]
public async Task<ActionResult<IEnumerable<FacultyInterestItems>>> GetFacultyInterestItems()
{
return await _context.FacultyInterestItems.ToListAsync();
}
这是模型:
五官Table
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 tables:
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; }
}
关键词table:
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()