我有一个blazor组件,我想传递从后端检索的数据。每组数据都有不同的实体名称,因此当它到达组件时,我无法将实体转换为泛型类型。我的错误在哪里?谢谢
在父数据库中的ChildData = @ sysList上获得以下错误:
Argument1:无法从Test2.Shared.Models.MeasurementSystem []转换为System.Collections.Generic.IReadOnlyList []
亲
<ChildComponent ChildData=@sysList [email protected]</ChildComponent>
<ChildComponent ChildData=@unitList [email protected]</ChildComponent>
<ChildComponent ChildData=@groupList [email protected]</ChildComponent>
@functions {
private Test2.Shared.Models.MeasurementSystem[] sysList { get; set; }
private Test2.Shared.Models.MeasurementUnit[] unitList { get; set; }
private Test2.Shared.Models.MeasurementGroup[] groupList { get; set; }
protected override async Task OnInitAsync()
{
sysList = await Http.GetJsonAsync<Test2.Shared.Models.MeasurementSystem[]>("/api/System/Index");
unitList = await Http.GetJsonAsync<Test2.Shared.Models.MeasurementUnit[]>("/api/Unit/Index");
groupList = await Http.GetJsonAsync<Test2.Shared.Models.MeasurementGroup[]>("/api/Group/Index");
}
}
儿童
@typeparam TItem
@foreach (var data in ChildData)
{
<p @data.Name>
}
@functions {
[Parameter] private IReadOnlyList<TItem>[] ChildData { get; set; }
}
类
public class MeasurementSystem
{
public int SystemId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
public class MeasurementUnit
{
public int UnitId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Factor { get; set; }
public int Magnitude { get; set; }
}
}
public class MeasurementGroup
{
public int GroupId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
试试这个:
[Parameter] private IReadOnlyList<TItem> ChildData { get; set; }
而不是这个:
[Parameter] private IReadOnlyList<TItem>[] ChildData { get; set; }
我不确定你要做什么...为什么不使用模板化组件?
但是,请尝试执行以下操作:
private List<Test2.Shared.Models.MeasurementSystem> sysList { get; set; }
代替:
private Test2.Shared.Models.MeasurementSystem[] sysList { get; set; }
和:
sysList = await Http.GetJsonAsync<List<Test2.Shared.Models.MeasurementSystem>>("/api/System/Index");
代替 :
sysList = await Http.GetJsonAsync<Test2.Shared.Models.MeasurementSystem[]>("/api/System/Index");
对其他类型执行相同的操作...如果这不起作用,请改用模板化组件。
这是什么:<p @data.Name>