.NET Core Blazor: How to get the Checkbox value if it is checked?

.net-core blazor c# html

Question

I am trying to find to get the checkbox value if it is checked using Blazor framework, but I couldn't find any method for it so far. When I put the binding in the checkbox, it is always checked. I couldn't figured it out how to get the checked value.

This is my code:

<input type="checkbox" id="addition" name="math" value="add" bind="@name" />
<label for="addition">Addition</label>

Popular Answer

Remove the value attribute:

<input type="checkbox" id="addition" name="math" bind="@name" />

Add this property to the @function block or a class derived from BlazorCoponent:

public bool name {get;set;}

Now the value of your check box is bound to the name property and you can access this property, which contains the value of the check box, to retrieve the check box's value, just as you access other properties.

Hope this helps...



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why