While I was developing a Blazor-based application I realized that Blazor doesn't have an out-of-the-box checkbox list control, so I thought why not create one of my own.In this blog I will give a step by step guide to create a Check Box List in Blazor.
Step 1
I will be using our Blazor Example project which I have created in my previous Blogs. This is about Futuristic Blazor application architecture. You can get the code from here.
Step 2
Open the BlazorComponents Project, right click on the Shared folder and select Option Add => Razor Component like below screenshot.
How To Create A Checkbox List In Blazor
Step 3
On selecting that option you will get the following screen, give the name of the component as CheckBoxList as shown in the screenshot.
How To Create A Checkbox List In Blazor
Step 4
Copy and paste the following code into the file,
  1. @typeparam TItem
  2. <div style=@Style>
  3. @if (Data != null)
  4. {
  5. foreach (var item in Data)
  6. {
  7. var Text = TextField?.Invoke(item);
  8. var Value = ValueField?.Invoke(item).ToString();
  9. bool Checked = false;
  10. if (SelectedValues.Contains(Value))
  11. {
  12. Checked = true;
  13. }
  14. <input type="checkbox" checked=@Checked
  15. @onchange="eventArgs => { CheckboxClicked(Value, eventArgs.Value); }" />
  16. @Text <br />
  17. }
  18. }
  19. @ChildContent
  20. </div>
  21. @code {
  22. //Data for the Checkbox
  23. [Parameter] public IEnumerable<TItem> Data { get; set; }
  24. // The field to be shown adjacent to checkbox
  25. [Parameter] public Func<TItem, string> TextField { get; set; }
  26. // The Value which checkbox will return when checked
  27. [Parameter] public Func<TItem, object> ValueField { get; set; }
  28. // CSS Style for the Checkbox container
  29. [Parameter] public string Style { get; set; }
  30. // Any childd content for the control (if needed)
  31. [Parameter] public RenderFragment ChildContent { get; set; }
  32. // The array which contains the list of selected checkboxs
  33. [Parameter] public List<string> SelectedValues { get; set; }
  34. //Method to update the selected value on click on checkbox
  35. public void CheckboxClicked(string aSelectedId, object aChecked)
  36. {
  37. if ((bool)aChecked)
  38. {
  39. if (!SelectedValues.Contains(aSelectedId))
  40. {
  41. SelectedValues.Add(aSelectedId);
  42. }
  43. }
  44. else
  45. {
  46. if (SelectedValues.Contains(aSelectedId))
  47. {
  48. SelectedValues.Remove(aSelectedId);
  49. }
  50. }
  51. StateHasChanged();
  52. }
  53. }
Let's understand the above code, the HTML part is basically code to generate the CheckBoxList on the basis of parameter Data which is a list of generic type TItem so that we can bind any object type list to the CheckBoxList. While creating the CheckBoxList the code also checks if the value is present in the SelectedValues list and marks the CheckBox Checked property based on that. This is basically for loading the checkbox list in edit mode.
I have taken these properties from SelectList control present in Blazorise UI library.The idea is to make this control part of that library some day.
Step 5
To use this CheckboxList control, create a new page Named CbListEx.razor in Pages folder following Step-2 & 3.
How To Create A Checkbox List In Blazor
Step 6
Right click on the Pages folder again and select Option Add => Class like below screenshot.
How To Create A Checkbox List In Blazor
Step 7
On selecting that option you will get the following screen, give the name of the class as CbListEx.razor.cs as shown in screenshot.
How To Create A Checkbox List In Blazor
The idea here is to create a code behind class for the razor page and when we give the same name with .cs extension Visual Studio automatically groups the files together as shown in the below screenshot,
How To Create A Checkbox List In Blazor
Step 8
Copy the following code in CbListEx.razor.cs file,
  1. using Microsoft.AspNetCore.Components;
  2. using System.Collections.Generic;
  3. namespace BlazorComponents.Pages
  4. {
  5. public class CbListExBase : ComponentBase
  6. {
  7. public List<Employee> EmployeeList { get; set; }
  8. protected List<string> SelectedIds = new List<string>();
  9. public List<Employee> ObjectList { get; set; }
  10. public string OutPutValue { get; set; }
  11. protected override void OnInitialized()
  12. {
  13. EmployeeList = GetMockEmployees();
  14. }
  15. protected void ShowSelectedValues()
  16. {
  17. OutPutValue = string.Join(",", SelectedIds.ToArray());
  18. StateHasChanged();
  19. }
  20. private List<Employee> GetMockEmployees()
  21. {
  22. var vSubOne = new Employee()
  23. {
  24. EmployeeId = 4,
  25. FirstName = "S Ravi",
  26. LastName = "Kumar",
  27. MobileNo = "9901091975",
  28. Email = "[email protected]"
  29. };
  30. var vSubTwo = new Employee()
  31. {
  32. EmployeeId = 6,
  33. FirstName = "Payal",
  34. LastName = "Jain",
  35. MobileNo = "9001091905",
  36. Email = "[email protected]"
  37. };
  38. var vSubThree = new Employee()
  39. {
  40. EmployeeId = 7,
  41. FirstName = "Alok",
  42. LastName = "Ojha",
  43. MobileNo = "900091905",
  44. Email = "[email protected]"
  45. };
  46. var vSubFour = new Employee()
  47. {
  48. EmployeeId = 9,
  49. FirstName = "Divya",
  50. LastName = "Bharti",
  51. MobileNo = "9111767895",
  52. Email = "[email protected]"
  53. };
  54. var vSubFive = new Employee()
  55. {
  56. EmployeeId = 10,
  57. FirstName = "Mayuri",
  58. LastName = "Kango",
  59. MobileNo = "9111000025",
  60. Email = "[email protected]"
  61. };
  62. var vSubSix = new Employee()
  63. {
  64. EmployeeId = 11,
  65. FirstName = "Tamraj",
  66. LastName = "Kilwish",
  67. MobileNo = "9251000025",
  68. Email = "[email protected]"
  69. };
  70. var vSubSeven = new Employee()
  71. {
  72. EmployeeId = 12,
  73. FirstName = "James",
  74. LastName = "Bond",
  75. MobileNo = "9521000025",
  76. Email = "[email protected]"
  77. };
  78. var vSubList = new List<Employee>
  79. {
  80. vSubOne, vSubTwo,
  81. vSubThree, vSubFour,
  82. vSubFive,vSubSix,
  83. vSubSeven
  84. };
  85. return vSubList;
  86. }
  87. }
  88. public class Employee
  89. {
  90. public long EmployeeId
  91. { get; set; }
  92. public string FirstName
  93. { get; set; }
  94. public string LastName
  95. { get; set; }
  96. public string FullName
  97. {
  98. get
  99. {
  100. return FirstName + " " + LastName;
  101. }
  102. }
  103. public string MobileNo
  104. { get; set; }
  105. public string Email
  106. { get; set; }
  107. }
  108. }
In the above code we are basically loading the CheckBoxList on page load with a list of Employee object which is a custom class. ShowSelectedValues method will be called on click of a button to show the selected values of the checkbox.
I have kept the Employee class in the same file just to save it; in the actual project it will be part of your Models Folder/project.
Step 9
Copy Paste the following code in CbListEx.razor file,
  1. @page "/CbListEx"
  2. @inherits CbListExBase
  3. <div class="col-12">
  4. <h3><b>CheckBox List Example </b></h3>
  5. <hr />
  6. <div class="col-12 row">
  7. <label class="col-2 font-weight-bold">CheckBox List :</label>
  8. <CheckBoxList Data="@EmployeeList"
  9. TextField="@((item)=>item.FullName)"
  10. ValueField="@((item)=>item.EmployeeId)"
  11. SelectedValues="@SelectedIds" />
  12. </div>
  13. <div class="col-12 row">
  14. <label class="col-2 font-weight-bold">Selected Ids :</label>
  15. <b>@OutPutValue</b>
  16. </div>
  17. <br />
  18. <div class="col-12 row">
  19. <span class="col-2"></span>
  20. <button class="form-control col-1 btn btn-primary" @onclick="ShowSelectedValues">Show Checked Values</button>
  21. </div>
  22. </div>
In the above code we are just showing the bound CheckBoxList, a button to show the selected values in a Label.
Step 10
Add the following code in NavMenu.razor inside the Shared folder to add a menu item for opening CbListEx.razor page.
  1. <li class="nav-item px-3">
  2. <NavLink class="nav-link" href="CbListEx">
  3. <span class="oi oi-list-rich" aria-hidden="true"></span> CheckBox List Example
  4. </NavLink>
  5. </li>
Now when you execute the application you will get the following output,
How To Create A Checkbox List In Blazor