CodeBeam.MudBlazor.Extensions icon indicating copy to clipboard operation
CodeBeam.MudBlazor.Extensions copied to clipboard

MudComboBox Search Selection Conversion Type Error

Open PlayerModu opened this issue 2 years ago • 2 comments
trafficstars

When using the MudComboBox with T of an object type, and using the default search box, I am seeing a type conversion error underneath the component when I select one or more of the options in the list. However I do not see this type conversion error if I scroll through and select the same objects from the list manually.

Error: "Conversion to type PersonDto not implemented"

I've tried to include the relevant code below:

 private IEnumerable<PersonDto> _peopleToOverwrite;
 private List<PersonDto> _PeopleList;
..
<MudComboBox T="PersonDto"
	@bind-SelectedValues="_peopleToOverwrite"
	Label="People"
	Clearable="true"
	ShowCheckbox="true"
	Editable="true"
	MultiSelection="true"
	Margin="Margin.Dense">
	@foreach (var item in _PeopleList)
	{
		<MudComboBoxItem Value="@item"
			Text="@("P0" + @item.Name+ " - " + @item.City)">
			@item
		</MudComboBoxItem>
	}
</MudComboBox>

Please can you advise? This is a hard-blocker to me using this component. Given the docs only show an example of a simple string type, I can't see if it's a me issue or a component issue.

Notes - I'm using the latest version of this library and MudBlazor

PlayerModu avatar Aug 01 '23 12:08 PlayerModu

Working try.* snippet which shows the issue. Posting on here for exposure incase anyone else has this issue and isn't on the Discord ☺️

<MudComboBox T="PracticeInformationDto"  @bind-SelectedValues="_practicesToOverwrite"
    Label="Practice"
    Clearable="true"
    ShowCheckbox="true"
    Editable="true"
    MultiSelection="true"
    Margin="Margin.Dense">
    @foreach (var item in _filteredPracticesToOverwrite)
    {
        <MudComboBoxItem Value="@item"
            Text="@("P0" + @item.PracticeCode + " - " + @item.PracticeName)">
            @item
        </MudComboBoxItem>
    }
</MudComboBox>

@code {
    public class PracticeInformationDto
    {
        public string PracticeCode { get; set; }
        public string PracticeName { get; set; }
    }
 
    private List<PracticeInformationDto> _filteredPracticesToOverwrite = new()
        {
            new PracticeInformationDto()
            {
                PracticeCode = "1234",
                PracticeName = "ABC"
            },
            new PracticeInformationDto()
            {
                PracticeCode = "4321",
                PracticeName = "CBA"
            },
        };

    private IEnumerable<PracticeInformationDto> _practicesToOverwrite;
}

PlayerModu avatar Aug 01 '23 17:08 PlayerModu

Updating:

Until this can be done automatically, this error can be resolved by passing a custom converter to the Converter property on the MudComboxBox

    Converter<PracticeInformationDto> _converter = new Converter<PracticeInformationDto>
    {
        SetFunc = value => value.PracticeName
    };

PlayerModu avatar Aug 02 '23 12:08 PlayerModu