我从外部 API 获取数据,代码如下所示(这部分很好):
@code {
IEnumerable<IDictionary<string, object>> data;
int count;
bool isLoading;
async Task LoadData(LoadDataArgs args)
{
isLoading = true;
var uri = new Uri("https://services.radzen.com/odata/Northwind/Employees")
.GetODataUri(filter: args.Filter, top: args.Top, skip: args.Skip, orderby: args.OrderBy, count: true);
var response = await new HttpClient().SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));
var result = await response.ReadAsync<ODataServiceResult<IDictionary<string, object>>>();
data = result.Value.AsODataEnumerable();
count = result.Count;
isLoading = false;
}
}
在下拉菜单中,我想显示 EmployeeID,但无法访问它(Data="@data.Employee.ID" 不正确,不确定要放什么才能使其正常工作)。
<RadzenDropDown Data="@data.EmployeeID" TextProperty="EmployeeID" ValueProperty="EmployeeID" Name="Dropdown1" TValue="string">
</RadzenDropDown>
谢谢!
问题在于数据属性是:
IEnumerable<IDictionary<string, object>> data;
应该是:
IEnumerable<Employee>data;
这样您就可以访问 Employee 类属性了。
<RadzenDropDown Data="@data.EmployeeID" TextProperty="EmployeeID" ValueProperty="EmployeeID" Name="Dropdown1" TValue="Employee">
</RadzenDropDown>
参加本教程,您将在文章末尾找到整个课程 https://www.c-sharpcorner.com/UploadFile/87b416/dynamically-create-a-class-at-runtime/#fromHistory
AppDomain.CurrentDomain.DefineDynamicAssembly
必须更改为 AssemblyBuilder.DefineDynamicAssembly
(为了与 .net core 和 .net 5+ 一起使用)
页面的示例代码。您将在字典上循环到 t.GetProperty("UserID").SetValue(...
@代码 { 字符串值{获取;放; } 列表数据 = new(); protected 覆盖异步任务 OnInitializedAsync() { MyClassBuilder MCB = new MyClassBuilder("ClsUser"); var myclass = MCB.CreateObject(new string[4] { "用户ID", "用户名", "生日", "位置" }, new Type[4] { typeof(string), typeof(string), typeof(DateTime) , typeof(字符串) }); 类型 t = myclass.GetType(); var obj = Activator.CreateInstance(t); t.GetProperty("UserID").SetValue(obj, "34", null); t.GetProperty("用户名").SetValue(obj, "阿尔伯特", null); t.GetProperty("生日").SetValue(obj, new DateTime(1976, 3, 14), null); t.GetProperty("位置").SetValue(obj, "KRK", null); 数据.Add(obj);
}
}