How to consume Web API OnInitialized in Blazor Child Component?

hkboy :

I'm new to C# and Blazor WASM in general and have struggled with this for quite some time.

I have a parent component "Parent.razor" and child component "Child.razor"

In parents, apart from all other HTML stuff, I also have child tag. So it looks something like (just for example)

Parent.razor
@page "/machines"

<h1>Machine Information<h1>
<child></child>

@code
{
}

Now, there is a foreach loop in child component that iterate each item in a list (machinetypes) If machinetypes is from a specified static list, it all works well. Screenshot for reference, I am able to get the dropdown that I want

Code below.

Child.razor
@using Namespace.Shared.Models
@using Blazorise
@inject HttpClient Http


<select>
    @foreach (var machinetype in machinetypes)
    {
        <option value="@machinetype.MachineTypeId">@machinetype.MachineTypeName</option>
    }
</select>

@code {
    public int amachinetypeid { get; set; }

    //This works
    MachineType[] machinetypes = new MachineType[]
    {
        new MachineType{MachineTypeName="TypeX"},
        new MachineType{MachineTypeName="TypeY"}
    };
}

But when I try to consume web API (that also returns list), it doesn't work. The page loads indefinitely and I will encounter and error. Code below.

Child.razor
@using Namespace.Shared.Models
@using Blazorise
@inject HttpClient Http


<select>
    @foreach (var machinetype in machinetypes)
    {
        <option value="@machinetype.MachineTypeId">@machinetype.MachineTypeName</option>
    }
</select>

@code {
public int amachinetypeid { get; set; }
public MachineType[] machinetypes;

//This works
//MachineType[] machinetypes = new MachineType[]
//{
//   new MachineType{MachineTypeName="TypeX"},
//    new MachineType{MachineTypeName="TypeY"}
//};

//This does not work    
protected override async Task OnInitializedAsync()
{
    machinetypes = await Http.GetJsonAsync<MachineType[]>("api/machinetype/");
}

}

Quick check on my web API, accessing it directly returns this to me. So it works alright. Click here to see screenshot of web API results

Could anyone advise what I did wrong? And is necessary to create an dependency injection a necessity to do this? If yes, could anyone please point me to a right direction?

Thank you in advance.

Henk Holterman :

You are probably having a null-reference error. That might look like "loads indefinitely".

What you need is

public MachineType[] machinetypes = new MachineType[0];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Consume POST Rest API on Django Web? want to consume API of User registration in django and here is the code which i have done

Cannot consume rest api (Web Api) from component React

How Web component set values to it's child?

Blazor: How to get sender from event in child component

How to write .net web api controller to consume and expose another web api

How can I pass a List<string> as a parameter to a child component in blazor?

how to call child component method from parent component in blazor?

How to get reference to the Blazor MainLayout from a child component

Blazor Child Component EventCallBack not responding

binding button to child component blazor

How do I handle an exception thrown in a Blazor child component in the component/page hosting it?

How to bind both ways to a select in a Child Component - Blazor

Consume web api in SSRS with parameter

Javascript HTML - how to consume web api

How to consume Web Api based on Json using Asp MVC 5?

How to create web api and consume from client application?

How to consume web api services hosted from angularJS

How to consume this api?

How to consume a asp.net web api in classic asp?

How do I call a method in one Blazor (parent) component from another Blazor ("child") component?

Why Return Base Method With Blazor OnInitialized Method

Blazor - how to remove a child component

Blazor - Avoid OnInitialized/OnInitializedAsync

Can we consume a Blazor component as a Web Component within a regular non-Blazor HTML page?

How to consume my authorized web api to my mvc web app

How can I pass List from child component to parent component in blazor?

Sveltekit - consume external REST api inside a component

Blazor WASM view not using a variable from OnInitialized()

Blazor child component event handling

TOP Ranking

HotTag

Archive