C
C#16mo ago
Ice_

❔ Blazor InputSelect, default value is null

I have a Blazor project and I am using an InputSelect and inside it a foreach loop to populate the list with fruit categories. When selecting one of the categories, I have another foreach loop that populates the fruits in the selected category. I then I have a button on this form to send an "order" with the fruitId. It works but I noticed that if I am happy with the first item in the list and don't actively choose the fruit and just press order, the value from the <option> will be null/0. So it seems the first item has no value. Until I change the fruit and then change it back to the first fruit. Then when ordering, I will get the fruitId. For now I've "fixed it" by adding an <option selected value="">Select fruit...<option>. Since I use EditForm with model binding, it will complain that, that particular option isn't valid if the user is trying to order. However, if the user selects a fruit category and just press order ("Select fruit..." will be default), the form will accept the data. So I guess I have two issues but if I somehow can fix the first issue, I won't need to add an extra option so the user is "forced" to actively choose a fruit. Any ideas how to fix this?
2 Replies
Ice_
Ice_16mo ago
It wasn't clear but it's two inputselect of course
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.