我无法访问导航属性,即使我已使用“包含”将其显式包含在查询中。奇怪的是,在代码后面访问时导航属性是存在的,但在 Blazor UI 中,它显示为 null。
`
TaskTypeModel.cs
public class TaskTypeModel
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public int SiteId { get; set; }
public SiteModel Site { get; set; } = null!;
}
AddTask.razor
<MudSelect T="string">
@if (taskTypes != null)
{
@foreach (var taskType in taskTypes)
{
<MudSelectItem Value="taskType.Site.Name">@taskType.Site.Name</MudSelectItem>
}
}
</MudSelect>
AddTask.razor.cs
IQueryable<TaskTypeModel>? taskTypes;
[Inject] public ApplicationDbContext dbContext { get; set; } = default;
protected override void OnInitialized()
{
taskTypes = dbContext.TaskTypes.Include(t => t.Site);
foreach (var taskType in taskTypes)
{
// It print out all site without error
Console.WriteLine(taskType.Site.Name);
}
}
预加载效果很好。
但是因为 Value 是字符串类型,所以我认为你需要的是
中的
@
<MudSelectItem Value="@taskType.Site.Name">