Async onchange with args and parameters run time error

I am trying to pass args as well as a parameter as async with onchange for a checkbox. I am getting this runtime error "Unhandled exception rendering component: Failed to execute 'setAttribute' on 'Element': '@onchange(async' is not a valid attribute name". Thanks in advance.


<SfCheckBox Disabled="@AdminIsDisabled" @bind-Checked="userDetails.AdminSelected" ID="checkboxAdmin" @onchange(async (Enums.Role.Admin, args) => await OnChangeRole())></SfCheckBox>

@code{
private async Task OnChangeRole(Enums.Role role, ChangeEventArgs args)
{
    bool isSelected = Convert.ToBoolean(args.Value);
    if (isSelected)
    {
        await SetRole((int)role, isSelected, true, true, false);
    }
    else
    {
        isSelected = true;
    }
}
}

4 Replies 1 reply marked as answer

YA YuvanShankar Arunagiri Syncfusion Team June 22, 2022 07:10 AM UTC

Hi David,


We have validated your reported query and prepared the sample code snippet based on your provided code snippet. Please pass arguments like below highlighted line.

@using Syncfusion.Blazor.Buttons

 

<SfCheckBox Disabled="@AdminIsDisabled" @bind-Checked="userDetails" ID="checkboxAdmin" @onchange="e => OnChangeRole(Dummy, e)"></SfCheckBox>

 

@code{

    private bool AdminIsDisabled = false;

    private bool userDetails = true;

    private Enums Dummy = new Enums { Role = "Admin" };

 

    private async Task OnChangeRole( Enums role, ChangeEventArgs args)

    {

 

    }

 

    public class Enums

    {

      public string Role { get; set; }

    }

}


Could you please check the above code and get back to us, if you need any further assistance on this. 


Regards,

YuvanShankar A


Marked as answer

DM David Morello June 22, 2022 02:24 PM UTC

That worked. Thank you so much. Quick question. I am knew to working with Blazor. Even though there is no await on the SfCheckBox this will still be one async?



DM David Morello replied to David Morello June 23, 2022 01:13 AM UTC

I figured out how to do this asynchronously. Adding it in case it helps others.


<SfCheckBox Disabled="@AdminIsDisabled" @bind-Checked="userDetails.AdminSelected" ID="checkboxAdmin" @onchange="@(async (args) => await OnChangeRole(Enums.Role.Admin, args))"></SfCheckBox>

And the method would look the same as above

private async Task OnChangeRole(UM.Common.Enums.Role role, ChangeEventArgs args)
{
}


YA YuvanShankar Arunagiri Syncfusion Team June 23, 2022 05:31 AM UTC

Hi David,


Thanks for your update. Please let us know if you have any other queries.


Regards,

YuvanShankar A


Loader.
Up arrow icon