Blazor On Parent Click then Check Child Checkbox

Viewed 238

This is what my html currently looks like.

<div class="form-check" @onclick="ChangeCheckboxState">
    <input type="checkbox" id="isActive" class="form-check-input" bind="@MyModel.Active" checked />
    <label class="form-check-label" for="isActive">Is Active?</label>
</div>

What I can't figure out how to do in blazor is on click for '.form-check' then check the child checkbox. Its easy using Javascript and/or JQuery. I am trying to avoid using JS if Blazor can handle it without too much hassle but doesn't seem to be possible.

I am using the code behind approach, creating a subclass of ComponentBase

1 Answers

I'm not sure what is your problem, but...

You should add @bind instead of just bind

<input type="checkbox" id="isActive" class="form-check-input" @bind="MyModel.Active" checked />

And do

void ChangeCheckboxState(){
    MyModel.Active = !MyModel.Active;
}
Related