MudBlazor icon indicating copy to clipboard operation
MudBlazor copied to clipboard

MudSelect: Text/value not updating correctly when ViewModel changes

Open IlyaNavodkin opened this issue 1 year ago • 4 comments

Things to check

  • [X] I have searched the existing issues for this bug
  • [X] To rule out a caching problem I made sure the bug also happens in an incognito tab

Bug type

Component

Component name

MudSelect

What happened?

After selecting a model for each view in two MudSelect data, the second MudSelect is not populated correctly when the active ViewModel changes. Text property (MudSelect) is not updating

Expected behavior

I would like both MudSelects to populate correctly when the active ViewModel changes.

Reproduction link

https://github.com/IlyaNavodkin/MudBlazortestApp/blob/master/MudBlazortestApp/Components/Pages/Counter.razor

Reproduction steps

1.Open "Count" Page image 2.Select "Group 1" image 3. Select for MudSelects data image 4.Select "Group 2" image 5. Select for MudSelects data image 6. Select "Group 1" and catch bug - incorrect value in secodary MudSelect image 7. Select "Group 2" and catch bug - incorrect value in secodary MudSelect FROM Group 1 image

Relevant log output

No response

Version (bug)

6.*

Version (working)

No response

What browsers are you seeing the problem on?

Chrome, Other

On which operating systems are you experiencing the issue?

Windows

Pull Request

  • [ ] I would like to do a Pull Request

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

IlyaNavodkin avatar Sep 02 '24 20:09 IlyaNavodkin

I solved the problem by forcing rerendering with the component text when changing the view model

public partial class GroupSelectionComponent
{
    [Parameter]
    public GroupViewModel SelectedGroup { get; set; }

    protected override Task OnParametersSetAsync()
    {
        if (ManufactureSelect is not null )
        ManufactureSelect.ForceRender(true);

        if (CatalogSelect is not null)
        CatalogSelect.ForceRender(true);

        return base.OnParametersSetAsync();
    }

    private MudSelect<ManufactureViewModel> ManufactureSelect;

    private MudSelect<CatalogViewModel> CatalogSelect;
}

IlyaNavodkin avatar Sep 03 '24 06:09 IlyaNavodkin

Do you have a reproduction link?

Anu6is avatar Sep 03 '24 14:09 Anu6is

@Anu6is Hello U can clone this repo https://github.com/IlyaNavodkin/MudBlazortestApp/blob/master/ And run this page https://github.com/IlyaNavodkin/MudBlazortestApp/blob/master/MudBlazortestApp/Components/Pages/Counter.razor

IlyaNavodkin avatar Sep 04 '24 15:09 IlyaNavodkin

Have also encountered this issue, reproduction link here try.mudblazor.com

In our case the MudSelect is falling back to rendering the object as a string, using ToStringFunc if it is specified otherwise presumably just calling on the object .ToString().

It appears to do this when we trigger a render after altering the list of items. In the above repro if you set the second MudSelect to World and then set the first to Hello, you'll see it fall back to displaying the ToStringFunc value. If you trigger another render it will fix itself

stewart-james avatar Mar 19 '25 15:03 stewart-james

I'm not exactly sure that it's the exact same issue, but I created a clean reproduction sample here: https://try.mudblazor.com/snippet/QEmJblOTRyyMrnaH

Note the visible text in the dropdown always being late with one update.

A ForceRender call still works as the workaround (also included in the sample), but the bug itself is fairly confusing. Please consider reprioritizing.

legrab avatar Nov 05 '25 09:11 legrab

Update

Unfortunately, even ForceRender may not always work.

When updating the list of SelectItems by removing the one that correlates with the current value, trhe ToStringFunc is called as expected, but instead of providing the return value, the content of the next SelectItem will be instead presented.

I made a reproduction of the buggy state that still uses OnParameterSet as the workaround but encounters the issue: https://try.mudblazor.com/snippet/mEGpFvlEyhARPMcN Try the Remove Selection button and see that instead of showing Not found 0, the visible value will be other text, thus eventually suggesting that the selection has the item with index 1 selected, which is not true.

However, I found the actual workaround which is cleaner than using OnParameterSet: use the localized label from the ToStringFunc as the key for the component to ensure it is always refreshed. This may result in multiple renders, but still is the simplest workaround.

I think it is clear, that this bug should still be looked into and fixed properly, but wanted to leave my remark to consider the workaround updated.

Here is the working equivalent with @key forced updates instead of @ref and ForceRender calls: https://try.mudblazor.com/snippet/cYwfPFFaSLXFBxLk

legrab avatar Nov 10 '25 13:11 legrab