Databinding CheckboxList

To start, I am a HUGE fan of your FREE library. :grin:

I was curious if anyone has any working code for databinding the CheckboxList with EntityFramework and/or what that would look like. For my purposes, I am wanting to bind a list of permissions with a User (as in IdentityUser) and be able to assign them by using a CheckboxList.

Thank you for any assistance you can provide here and thank you for this incredible library! I am very impressed with it... reminds me of the Silverlight control toolkit back in the day. :+1:

Hi @DragonSpark,

The value of the component can be bound as in our example however if you want to populate items from database you will need to manually loop your database values and add the items:

        <RadzenCheckBoxList @bind-Value="values" TValue="int">
            <Items>
                @foreach(var item in yourList)
                {
                    <RadzenCheckBoxListItem Text="@item" Value="@item" />
                }
            </Items>
1 Like

Thank you @enchev for your answer and all your efforts here.

This provides one part of my answer. Now what I need to do is bind it to the list of already provided values within a user's list of permissions contained within a Permissions property collection. Checking an item will add the permission to the collection, and unchecking it will remove it. This seems a little different from above, so if you have any additional resources here that would demonstrate how to do something like this, it would be greatly appreciated and respected!

1 Like

I have exactly the same scenario,
There seems to be no indication as to how to bind the actual value indicating if the checkbox should be checked or not depending if the user has the permission.
Please advise

@enchev @korchev

1 Like

I have a similar problem:

Option = {string name, bool Selected, bool Emergency}
List[Option] options;

@foreach (var option in options)
{
RadzenCheckBoxList @bind-Value="Options" TValue="Option"
Items
RadzenCheckBoxListItem Text="Selected" Value="@option.Selected"
RadzenCheckBoxListItem Text="Emergency" Value="@option.Emergency"
/Items
RadzenCheckBoxList
}

So how do I make a binding to an Option property?

Our online demo shows how to set the value of a CheckBoxList - through the Value property.

<RadzenCheckBoxList @bind-Value="values" TValue="int">
    <Items>
        <RadzenCheckBoxListItem Text="Orders" Value="1" />
        <RadzenCheckBoxListItem Text="Employees" Value="2" />
        <RadzenCheckBoxListItem Text="Customers" Value="3" />
    </Items>
</RadzenCheckBoxList>
@code {
 // The Value of the option(s) that will be checked
 IEnumerable<int> values = new int[] { 1 };
}

Do you have any sample code on how to bind CheckBoxList items from List Object?

@jdharma007 this thread already contains an example here.

@using System.Linq;

private List<OptionModel> options; // populate from db. IsChecked is bool. OptionID is int.
private IEnumerable<int> values;

protected override async Task OnInitializedAsync()
 {
    options = await _db.GetThemFromDB();
    // values should contain a list of checked IDs from the database
    values = (IEnumerable<int>)options.Where(s => s.IsChecked == true).Select(s => s.OptionID) 
}
private void Change(object args)
{
        values = (List<int>)args; 
        // will contain list of IDs for checked/selected items for you to do as you wish in DB
}
 <RadzenCheckBoxList @bind-value=@values TValue="int" Change=@(args => Change(args))>
 <Items>
                    @foreach (OptionModel item in options)
                    {
                        <Radzen.Blazor.RadzenCheckBoxListItem Text="@item.DisplaySomething" Value="item.OptionID"></Radzen.Blazor.RadzenCheckBoxListItem>
                    }          
            </Items>
</RadzenCheckBoxList>
2 Likes

@enchev How do I enter the foreach loop so that it doesn't get overwritten by Radzen when I make changes to the form?