2

In my Blazor server-side application, I have a component with a button that, when clicked, displays a "Loading" bar while I check to make sure a all of my required text fields are not blank.

<RadzenButton Shade="Shade.Dark" Click=@(args => OnClickSubmit()) Text="Submit Application" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Large" />

This leads into my Task OnClickSubmit that runs a StateHasChanged, then checks the applicable fields.

    private bool IsLoading = false;
    private bool errors = false;
    private string ErrorMessage = string.Empty;

    public async Task OnClickSubmit()
    {
        IsLoading = true;
        StateHasChanged();
        CheckFields();
        if (ErrorMessage == "")
        {
            //Success
        }
        else
        {
            errors = true;
    
        }
        IsLoading = false;
        StateHasChanged();
    }

    public void CheckFields()
    {
        ErrorMessage = "";
    
        //Check Basic Fields
        if(FirstName == "")
        {
            ErrorMessage = "First Name is required." + "\n";
        }
    
        if (LastName == "")
        {
            ErrorMessage = ErrorMessage +  "Last Name is required." + "\n";
        }
    }

But the page does not appear to be refreshing at all. I debugged my code, and everything appears to be populating and hitting where you'd expect. But the page itself just isn't display the loading, nor does it show my ErrorMessage that should now appear since my boolean is switched to true.

I run this same "loading" StateHasChanged process on firstRender, but I'm not sure why it doesn't appear to be working here. Any help would be appreciated.

1 Answer 1

1

Currently your code is applying the UI update after the whole method is executed. Since the isLoading is setting false just before the execution is end and also for the first StateHaschanged() method, there is CheckFields(); which are synchronous. So executing everything together. Either you can make the CheckFields(); asynchronous and put await before that. Or you can await Task.Yield();

Solution 1:

public async Task OnClickSubmit()
{
        IsLoading = true;
        StateHasChanged();
        await CheckFields();
        if (ErrorMessage == "")
        {
            //Success
        }
        else
        {
            errors = true;
    
        }
        IsLoading = false;
        StateHasChanged();
}



Solution 2:

public async Task OnClickSubmit()
{
    IsLoading = true;
    StateHasChanged();
    await Task.Yield(); 
    CheckFields();

    if (string.IsNullOrEmpty(ErrorMessage))
    {
        // success
    }
    else
    {
        errors = true;
    }

    IsLoading = false;
    StateHasChanged();
}
New contributor
Asif Rabbani is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.