Blazorise icon indicating copy to clipboard operation
Blazorise copied to clipboard

DefaultItemValue for Selectlist does not work for null

Open tacoWanneeJama opened this issue 2 years ago • 2 comments

If you make a Selectlist with DefaultItemText & DefaultItemValue you expect that when the customer first clicks on a row and ten back to the default row, that the SelectedValue is filled with DefaultItemValue . For a DefaultItemValue like -1 this works ok, but for a DefaultItemValue of null the SelectedValue has the content of the previously selected row.

I did not check if SelectedValueChanged was called in both cases.

<Validation Validator="ws4.Client.ValidationRule.IsSelectFilled">
        <SelectList TItem="RemovalReason"
                       TValue="int?"
                        Data="@(_removalReasons.Where(rr=> rr.DeviceTypeId == _device.DevicetypeId))"
                        TextField="@((item)=> item.Name)"
                        ValueField="@((item)=> item.Id.Value)"
                        SelectedValue="_deviceRemoval.RemovalReasonId"
                        SelectedValueChanged="@((nv)=> _deviceRemoval.RemovalReasonId = nv)"
                        DefaultItemText="Selecteer een reden" 
                        DefaultItemValue="-1" />
</Validation>

My current workaround is to use -1 as DefaultItemValue instead of null

tacoWanneeJama avatar Jun 30 '22 08:06 tacoWanneeJama

This is practically a limitation in browsers. When you have null or empty as an <option> value then it will return the option content as a value. It's a known problem. Usually, it helps to have both value and content as an empty. eg, <SelectItem Value="@((int?)null)"></SelectItem>

@David-Moreira we should really consider changing the internals of the Select component. Maybe we can keep an internal list of items instead of depending on browser behavior. Something similar to what we already do with DataGrid.

stsrki avatar Jun 30 '22 09:06 stsrki

Right somehow uniquely identify each item preferably with a string based identifier. This is turning out to be a much needed refactor.

Also, light read for related Select component and these cases: https://blazorise.com/blog/how-to-handle-select-with-primitive-and-complex-types

David-Moreira avatar Jul 18 '22 22:07 David-Moreira