Tobias Madsen
Tobias Madsen

Reputation: 39

How to update multiple properties when one prop is changing

I have a class with a property Area and another property Department.

I want to update The Department when the Area property is changing.

I am using Mudblazor Autocomplete @bind-Value but this method is only updating the bound prop which is Area.

One solution could be to mutate the setter in the area prop but this is not a solid solution in my opinion.

Form.cs

public DepartmentResponse Department
{
 get;
 set;
}
public AreaResponse Area
{
 get;
 set;
}

RazorPage

<MudAutoComplete 
@bind-Value="ViewModel.Area"
/>

<MudAutoComplete 
@bind-Value="ViewModel.Department"
/>

So when I change the Area I want to set the department to first department that match with the areaId.

Upvotes: 0

Views: 358

Answers (1)

Quickz
Quickz

Reputation: 1846

If you wish to avoid modifying the setter of the Area property, feel free to use ValueChanged event instead of the @bind-Value.

Example:

https://try.mudblazor.com/snippet/QaQQYWOruEaCHJbr

<MudText Color=Color.Success class="mt-3">Selected: @area</MudText>
<MudAutocomplete T=string SearchFunc=@Areas ValueChanged=@OnAreaChanged />

@code {
    private string area;
    private string[] AvailableAreas = { "Area 1", "Area 2", "Area 3", };

    private Task<IEnumerable<string>> Areas(string value)
    {
        return Task.FromResult(AvailableAreas.AsEnumerable());
    }

    private void OnAreaChanged(string newArea)
    {
        area = newArea;
        // More code
    }
}

Upvotes: 1

Related Questions