Home > database >  Unable to load Data in NavMenu
Unable to load Data in NavMenu

Time:10-08

When I try to get values from Database for my NavMenu it seems that the values aren`t available fast enought.

NavMenu Html:

<h2>test</h2><h2>@titleList.Single(x => x.name == "test2").value</h2>

NavMenu code:

protected override async Task OnInitializedAsync()
        {
            titleList = await client.GetFromJsonAsync<List<NavMenuStringModel>>("api/navMenuStrings");
        }

Controller:

[HttpGet]
    public async Task<IActionResult> Get()
    {
        List<NavMenuStringModel> navMenuStringModels = new List<NavMenuStringModel>()
        {
            new NavMenuStringModel(){name = "test2", value="test2"}
        };
        return Ok(navMenuStringModels);
    }

->Unhandelt exeption in Browser. ->Console shows "No element is currently associated with component 8"

CodePudding user response:

Although you didn't show it, I'm assuming titlelist is initialized as null.

So you probably need something like this:

<h2>test</h2>
@if(titlelist != null)
{
    <h2>titleList.SingleOrDefault(x => x.name == "test2").value</h2>    
}

I also suggest SingleOrDefault in case there is no matching element.

Finally you may need to call StateHasChanged() after you populate titlelist.

  • Related