Radzen Blazor DropDown on dynamic data from external API

2.6k Views Asked by At

I'm getting data from an external API and the code looks like this (this part is fine):

@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;
    }
}

On the dropdown menu I want to display the EmployeeID, but cannot access it (the Data="@data.Employee.ID" is incorrect and not sure what to put in there to make it work).

<RadzenDropDown Data="@data.EmployeeID" TextProperty="EmployeeID" ValueProperty="EmployeeID" Name="Dropdown1" TValue="string">
            </RadzenDropDown>

Thanks!

2

There are 2 best solutions below

0
On

The problem is that the Data Property is:

 IEnumerable<IDictionary<string, object>> data;

It should be:

IEnumerable<Employee>data;

So then you can acess to Employee class properties.

<RadzenDropDown Data="@data.EmployeeID" TextProperty="EmployeeID" ValueProperty="EmployeeID" Name="Dropdown1" TValue="Employee">
            </RadzenDropDown>
0
On
  1. Take this tutrial, you'll find the whole class in the end of the article https://www.c-sharpcorner.com/UploadFile/87b416/dynamically-create-a-class-at-runtime/#fromHistory

  2. AppDomain.CurrentDomain.DefineDynamicAssembly must be changed to AssemblyBuilder.DefineDynamicAssembly (in order to work with .net core and .net 5+)

  3. Example Code of the page. You'll loop on your Dictionary to t.GetProperty("UserID").SetValue(...

    @code { string value { get; set; } List data = new(); protected override async Task OnInitializedAsync() { MyClassBuilder MCB = new MyClassBuilder ("ClsUser"); var myclass = MCB.CreateObject(new string[4] { "UserID", "Username", "Birthday", "Location" }, new Type[4] { typeof(string), typeof(string), typeof(DateTime), typeof(string) }); Type t = myclass.GetType(); var obj = Activator.CreateInstance(t); t.GetProperty("UserID").SetValue(obj, "34", null); t.GetProperty("Username").SetValue(obj, "Albert", null); t.GetProperty("Birthday").SetValue(obj, new DateTime(1976, 3, 14), null); t.GetProperty("Location").SetValue(obj, "KRK", null); data.Add(obj); } }