Open Tornhoof opened 7 months ago
You may need nullable:
public string[]? SelectedRoles { get; set; } = [];
You may need nullable:
Yeah, I tried that, doesn't help, still throws the same exception.
@Tornhoof The Binding for the InputSelect
with no items selected is thowing the exception in the internal BindConverter
class. The MakeArrayFormatter
method is expecting a non-nullable array. You need to add Validation to disallow no selection. You can read more about Validation here: Determine if a form field is valid
@gragra33 I'm aware that I can limit the selection to need more than 0 selections, all the examples (and tests afaik) do that. But that kinda defeats the point of it :) I'm also aware that I can cheat with some empty string value or conceptionally None Item or something similar, but that also defeats the whole point. I'm also aware that I can cheat with writing my own component, use input with multiple directly or other methods to bypass the issue or some other method.
My workaround, as described above, is a lot cleaner than those though.
There is a lot of strange (atleast for me) documentation around null and model binding, in this case I simply expect, that for an empty selection input, it creates an empty array for the model binding. I personally think, that empty selections were simply missed during development.
Note: In my attempts to repro #54432 I found out, that the issue only happens for SSR, as soon as @rendermode InteractiveServer is added the array is not null, but empty.
Is there an existing issue for this?
Describe the bug
If there is an InputSelect in an EditForm with multiple values (via array) and nothing is selected (or everything is deselected) the following exception is thrown:
Assuming the following model
And the following markup:
Repo see below, markup is here: https://github.com/Tornhoof/InputMultiSelectRepro/blob/main/Components/Pages/Home.razor
Expected Behavior
No such exception
Workaround is to set the array to [] in
OnInitializedAsync
Steps To Reproduce
Exceptions (if any)
.NET Version
8.0.200
Anything else?
No response