diff --git a/Pages/Demos/Checkboxes.cshtml b/Pages/Demos/Checkboxes.cshtml index 6e4a1c1..52bec23 100644 --- a/Pages/Demos/Checkboxes.cshtml +++ b/Pages/Demos/Checkboxes.cshtml @@ -80,6 +80,24 @@ } +
+

+ It's also common to bind a boolean property from a list to checkboxes. +

+ @for(var i = 0; i + + @number.Name + + + @* hidden for IsSelected is added automatically *@ + } + + Selected numbers: @string.Join(", ", Model.Numbers.Where(n => n.IsSelected).Select(n => n.Name)) +
+ diff --git a/Pages/Demos/Checkboxes.cshtml.cs b/Pages/Demos/Checkboxes.cshtml.cs index 2de24a2..7e455fc 100644 --- a/Pages/Demos/Checkboxes.cshtml.cs +++ b/Pages/Demos/Checkboxes.cshtml.cs @@ -8,6 +8,13 @@ public class Checkboxes : PageModel { public string? StatusMessage { get; set; } + public void OnGet() + { + Numbers.Add(new() { Name = "One" }); + Numbers.Add(new() { Name = "Two" }); + Numbers.Add(new() { Name = "Three" }); + } + public IActionResult OnPost() { StatusMessage = "Form was submitted: " + (ModelState.IsValid @@ -36,4 +43,13 @@ public class InputModel { public bool IsChecked { get; set; } } + + [BindProperty] + public List Numbers { get; } = new(); + + public class Selectable + { + public required string Name { get; set; } + public bool IsSelected { get; set; } + } } \ No newline at end of file