jeudi 15 septembre 2022

How can I retrieve multiple checkbox values from checkboxes added dynamically in Blazor when using EditForm and/or @bind-Value?

I am trying to find out how to get checkbox values when checked in a form in Blazor.

I can see from this post (.NET Core Blazor: How to get the Checkbox value if it is checked? ), how to do it for a single value:

a solution to the above post gave me something like this:

<InputCheckbox @bind-Value="@b.myBool" checked="@(@b.myBool?"checked":null)" />CheckMe

and that works ok. I just create a class with a boolean property (say myBool) and declare an instance of the class with that property set to false. Then when the form is submitted, I have access to b.myBool, it is changed by the user.

However, I need to do it for multiple Checkboxes that will be added dynamically (the text next to the checkbox, here CheckMe[z], would also change but that's not an issue).

So, I figured a for/foreach loop with something like:

<InputCheckbox @bind-Value="@b.myBool[i]" checked="@(@b.myBool[i]?"checked":null)" />CheckMe[z]

and in the class, I'd just need to replace with a list property e.g.: List myBools.

Unfortunately this and lots of other variations I've tried don't work, for many many different reasons.

Thanks in advance for advice/suggestions/links.




Aucun commentaire:

Enregistrer un commentaire