blazormudblazor

Any solution when @bind-Value and ValueChanged don't work together?


I have a Data Grid that when selecting a row, that row will be saved as an rSelect object with @bind-Value. To be able to edit the object, I @bind-Value rSelect with select, where each selection has an id value but displays a name. Now I want it to display the id and the name of the object with that id to be displayed in the MudField below. After consulting the documentation I decided to use ValueChanged but it cannot run with @bind-Value, what should I do?

<MudItem xs="6" md="6">
    <MudSelect T="string" ReadOnly="@_readonly" Dense="true" @bind-Value="@rSelect.Manvcongno" Label="ID" Variant="Variant.Outlined">
        @foreach(var nv in nhanviens)
        {
            <MudSelectItem Value="@nv.id">@nv.Name</MudSelectItem>
            
        }
    </MudSelect>
</MudItem>
<MudItem xs="6" md="6">
    <MudField Variant="Variant.Outlined" Label="Name"></MudField>
</MudItem>

Solution

  • @bind-Value:after exists for this purpose. It's called after the bound value is updated.

    <MudItem xs="6" md="6">
        <MudSelect T="string" ReadOnly="@_readonly" Dense="true" @bind-Value="@rSelect.Manvcongno" @bind-Value:after="this.OnAfterChanged" Label="ID" Variant="Variant.Outlined">
            @foreach(var nv in nhanviens)
            {
                <MudSelectItem Value="@nv.id">@nv.Name</MudSelectItem>
                
            }
        </MudSelect>
    </MudItem>
    <MudItem xs="6" md="6">
        <MudField Variant="Variant.Outlined" Label="Name"></MudField>
    </MudItem>
    
    
    //.....
    
    private void OnAfterChanged()
    {
        // rSelect.Manvcongno has the new value here
        // do what you want
    }