OnParametersSetAsync is being called when i change te value of BootstrapSelect

60 views Asked by At

I have a selectbox using the following lib: https://github.com/Rob-Newman/blzr.bootstrap-select

When the Parameters are set i fill the selextbox with the available options and the already selected options.

When i open the selectbox and select a value OnParametersSetAsync is called for some reason. This resets the value of the selectbox.

I don't know why OnParametersSetAsync is called, maybe a bug?

@if (Calculators.Any())
{
    <div class="form-group">
        <br>
        <label for="CalculatorId">Calculators:</label>
        <BootstrapSelect @bind-Value="ChosenCalculators" ShowPlaceholder="true" PlaceholderText="Selecteer caculator(s)" ShowSearch="true" IsMultiple="true" TItem="Calculator" Data="@Calculators" TextField="@((item) => item.Name)"
                     ValueField="@((item) => item.Id.ToString())" TType="IEnumerable<int>" />
    </div>
}


<div class="my-2">
    <SpinnerButton IsSubmitting="@isSubmitting" ButtonText="@ButtonText" />
    <a @onclick="DeleteProjectDay" class="btn btn-outline-danger">Verwijderen</a>
</div>



@code {
    [Parameter]
    public EventCallback OnSaveCallBack { get; set; }
    [Parameter]
    public ProjectDay ProjectDay { get; set; }

    private bool isSubmitting = false;
    private SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
    private List<Calculator> Calculators { get; set; } = new List<Calculator>();
    private string ButtonText = "Toevoegen";
    private IEnumerable<int> ChosenCalculators { get; set; } = new List<int>();


    protected override async Task OnParametersSetAsync()
    {
        if (ProjectDay.Id != 0) ButtonText = "Bewerken";
        Calculators = await GetCalculatorsExcludingOffDayUseCase.ExecuteAsync(ProjectDay.DateTime);
        ChosenCalculators = (await GetProjectDayCalculatorsByProjectDayIdUseCase.ExecuteAsync(ProjectDay.Id)).Select(x => x.CalculatorId);
    }
}
1

There are 1 answers

0
MrC aka Shaun Curtis On

@bind-Value="ChosenCalculators" is Razor.

It gets compiled into a setter that writes the value provided by the ValueChanged EventCallback to ChosenCalculators. The Renderer treats this callback as a UI event and renders the parent component. As BootstrapSelect has several reference Parameters, the Renderer calls SetParametersAsync on BootstrapSelect.... which calls OnPatametersSet{Async}.

This looks like the code should be in OnInitializedAsync. See: How does Blazor know when to look for a parameter change