To get the checkbox value when it is checked or unchecked use the onchange event by calling a method in onchange event using lambda expression and passing the checkbox value to it.
@foreach (var check in CheckBoxList())
{
<input class="custom-checkbox" type="checkbox" @onchange="eventArgs => { CheckboxClicked(check, eventArgs.Value); }" />
@check
<br />
}
@code {
public List<string> CheckBox { get; set; } = new List<string>();
void CheckboxClicked(string CheckID, object checkedValue)
{
if ((bool)checkedValue)
{
if (!CheckBox.Contains(CheckID))
{
CheckBox.Add(CheckID);
}
}
else
{
if (CheckBox.Contains(CheckID))
{
CheckBox.Remove(CheckID);
}
}
}
public List<String> CheckBoxList()
{
List<String> checkBox = new List<String>();
checkBox.Add("CheckBox 1");
checkBox.Add("CheckBox 2");
return checkBox;
}
}
Share with