asp.net-coreblazor

img onerror event does not change property on view model


I'm trying to set a flag when the the image fails to load in blazor but it seems that the flag is never updated. What am I doing wrong ?

The fiddle looks like this:


<h1>Hello, world!</h1>

Welcome to your new app.

<img src="https://images.dog.ceo/breeds/dane-great/n02109047_8090" onerror="this.onerror=null; @(() => SetImageFailedFlag())">

<p>Image failed to load @ImageFailedFlag</p>

@code {
    private bool ImageFailedFlag  {get;set;} = false;

    private void SetImageFailedFlag() {
        ImageFailedFlag = true;
        StateHasChanged();
    }
}

Solution

  • To trigger the image load error event in blazor, it should be

    <img src="xxx" @onerror="SetImageFailedFlag">