1

I have the following code which creates a checkbox that looks like a toggle button.

<div class="btn-group btn-group-toggle" data-toggle="buttons">
 <label asp-for="FlaggedStatus" class="btn btn-outline-danger">
   <input asp-for="FlaggedStatus" type="checkbox"> Flag Address
 </label>
</div>

Which looks like this on the form unchecked:

enter image description here

and then like this when selected:

enter image description here

The binding works great for saving data. But when I load the form with data already entered, for edit mode, the checkbox is selected in the background, but the button does not show as filled in. So how do I bind it so the button will show as toggled on when the checkbox data is true?

1 Answer 1

1

The toggle uses the active css class on the parent label. Your going to have to use some inline logic:

<label asp-for="FlaggedStatus" class="btn btn-outline-danger @(Model.FlaggedStatus ? "active")">
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thanks. Just needed to add :"" to like @(Model.FlaggedStatus ? "active":"")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.