Razor 页面中未显示多个相关表的数据
Posted
技术标签:
【中文标题】Razor 页面中未显示多个相关表的数据【英文标题】:Data of Multiple related tables not shown in Razor page 【发布时间】:2020-01-02 08:04:43 【问题描述】:我正在使用 .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;
没有从数据库中获取教师和关键字的数据。请告诉我解决方案
【问题讨论】:
【参考方案1】:尝试调用Include
方法
_context.FacultyInterestItems.Include(x => x.Faculty).Include(x => x.Keyword).ToListAsync()
【讨论】:
我这样做了它在这条线上给出了错误 FacultyInterestItem = JsonConvert.DeserializeObject>(result); JsonSerializationException:反序列化数组时意外结束。路径“[0].faculty.facultyInterestItems”,第 1 行,位置 207。 解决了!谢谢马丁,问题出在我生成的模型上,我在它工作时删除了它 public virtual ICollection以上是关于Razor 页面中未显示多个相关表的数据的主要内容,如果未能解决你的问题,请参考以下文章
页面浏览访问路径报告在 COBUB RAZOR 中始终显示“无数据”