Closed CashPJ closed 7 months ago
Hi @CashPJ
The issue lies with the selected
attribute on the <option>
inside the <select>
HTMLElement . The browser only respects the selected
attribute on the initial rendering of the <select>
See:
So when you are rerendering the input by supplying it a new list of selected
attribute.
If you want it to select a specific item when you change the list you can either call a javascript function to select the new item, or force Blazor to destroy the old <select>
and create a new <select>
so it honors the selected attribute again.
You could do this for example by adding @key=ListSource
to your input
Hi @MariovanZeist
@key
mechanic solves the stated problem and allows to avoid use of javascript.I initially solved that problem with javascript, but I consider it a quick fix. @key
the mechanic for destroying the selected element you mentioned works, but it requires the developer to know about it.
In my opinion, it would be ideal to re-databind values when changing the list, which would cover the scenarios of dynamic drop downs that are common thing in the world of web applications. But that's just my opinion :)
Thanks and best regards!
If you use @bind
or @bind-Value
, that's supposed to take control over which option is selected. You can't also manually assign selected
attributes since that can be inconsistent with what @bind
is doing - the two will clash.
Is there a reason you can't restrict yourself to using @bind
/@bind-Value
to determine which option is selected?
@SteveSandersonMS, it was only for testing clarity. I noticed that "selected" attribute is ignored. Its existence could cause problems? I can change it into some "dummy" attribute which will be ignored by browsers.
I can change it into some "dummy" attribute which will be ignored by browsers.
If you're not using it to control selection then that would be wise. Otherwise it will be difficult to reason about since you have two different things both trying to control selection in possibly inconsistent ways.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.
Is there an existing issue for this?
Describe the bug
In my case, I want to show the bound object whether it exists in the list or not. And it works as expected. But when I change the list and there is a bound object in the list, the html is generated as expected, but the selected object is not selected in html select control.
Expected Behavior
Expected behavior wold be to have selected option in html control as shown in image
Steps To Reproduce
Example project is available at https://github.com/CashPJ/InputSelectBlazorBug
Steps to reproduce:
Exceptions (if any)
No response
.NET Version
NET 8 (8.0.101)
Anything else?
Visual studio 2022 - Version 17.8.4 dotnetinfo.txt