From ae597e87bf526e43cac922253f5380aa58517193 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Mon, 19 Jul 2021 09:49:00 -0500 Subject: [PATCH 1/5] Binding select multiple --- aspnetcore/blazor/components/data-binding.md | 86 +++++++++++++++- .../blazor/components/event-handling.md | 6 +- aspnetcore/blazor/forms-validation.md | 99 ++++++++++++++----- 3 files changed, 163 insertions(+), 28 deletions(-) diff --git a/aspnetcore/blazor/components/data-binding.md b/aspnetcore/blazor/components/data-binding.md index 73437b17c98e..101c9b54a8d4 100644 --- a/aspnetcore/blazor/components/data-binding.md +++ b/aspnetcore/blazor/components/data-binding.md @@ -75,6 +75,90 @@ Razor attribute binding is case sensitive: * `@bind` and `@bind:event` are valid. * `@Bind`/`@Bind:Event` (capital letters `B` and `E`) or `@BIND`/`@BIND:EVENT` (all capital letters) **are invalid**. +::: moniker range=">= aspnetcore-6.0" + +## Multiple option selection with `` elements + +Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attributes/multiple) option selection with `` elements. The [`@onchange`](xref:mvc/views/razor#onevent) event provides an array of the selected elements via [event arguments (`ChangeEventArgs`)](xref:blazor/components/event-handling#event-arguments). The value must be bound to an array type. + +`Pages/BindMultipleInput.razor`: + +```razor +@page "/bind-multiple-input" + +

Bind Multiple inputExample

+ +

+ +

+ +

+ Selected Cars: @string.Join(", ", SelectedCars) +

+ +

+ +

+ + + Selected Cities: @string.Join(", ", SelectedCities) + + +@code { + public string[] SelectedCars { get; set; } = new string[] { }; + public string[] SelectedCities { get; set; } = new[] { "\"sf\"", "\"sea\"" }; + + void SelectedCarsChanged(ChangeEventArgs e) + { + SelectedCars = (string[])e.Value; + } +} +``` + +For information on how empty strings and `null` values are handled in data binding, see the [Binding `` element options to C# object `null` values + +There's no sensible way to represent a ``'s value. + +::: moniker-end + +::: moniker range="< aspnetcore-5.0" + +The Blazor framework doesn't automatically handle `null` to empty string conversions when attempting two-way binding to a `` to a null value (dotnet/aspnetcore #23221)](https://github.com/dotnet/aspnetcore/pull/23221). + +::: moniker-end + ## Unparsable values When a user provides an unparsable value to a databound element, the unparsable value is automatically reverted to its previous value when the bind event is triggered. @@ -325,5 +409,5 @@ For an alternative approach suited to sharing data in memory and across componen * * [Binding to radio buttons in a form](xref:blazor/forms-validation#radio-buttons) -* [Binding `` element options to C# object `null` values - -There's no sensible way to represent a ``'s value. - -::: moniker-end - -::: moniker range="< aspnetcore-5.0" - -The Blazor framework doesn't automatically handle `null` to empty string conversions when attempting two-way binding to a `` to a null value (dotnet/aspnetcore #23221)](https://github.com/dotnet/aspnetcore/pull/23221). - -::: moniker-end - ## Validation Summary and Validation Message components The component summarizes all validation messages, which is similar to the [Validation Summary Tag Helper](xref:mvc/views/working-with-forms#the-validation-summary-tag-helper): From a36cd15af87ce560bf9c238092f50fd26c05b60b Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Tue, 20 Jul 2021 04:13:27 -0500 Subject: [PATCH 2/5] Updates --- aspnetcore/blazor/components/data-binding.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/aspnetcore/blazor/components/data-binding.md b/aspnetcore/blazor/components/data-binding.md index 101c9b54a8d4..31584d9f3dd4 100644 --- a/aspnetcore/blazor/components/data-binding.md +++ b/aspnetcore/blazor/components/data-binding.md @@ -92,10 +92,11 @@ Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attrib

@@ -108,9 +109,10 @@ Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attrib @@ -122,7 +124,7 @@ Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attrib @code { public string[] SelectedCars { get; set; } = new string[] { }; - public string[] SelectedCities { get; set; } = new[] { "\"sf\"", "\"sea\"" }; + public string[] SelectedCities { get; set; } = new[] { "\"bal\"", "\"sea\"" }; void SelectedCarsChanged(ChangeEventArgs e) { From b7746c905c027d655729026eee0cb5b399857f20 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Wed, 21 Jul 2021 21:20:13 -0500 Subject: [PATCH 3/5] Update aspnetcore/blazor/components/data-binding.md Co-authored-by: Mackinnon Buck --- aspnetcore/blazor/components/data-binding.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aspnetcore/blazor/components/data-binding.md b/aspnetcore/blazor/components/data-binding.md index 31584d9f3dd4..3f271e05bdb0 100644 --- a/aspnetcore/blazor/components/data-binding.md +++ b/aspnetcore/blazor/components/data-binding.md @@ -109,11 +109,11 @@ Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attrib

From d2d57531fe1d5e6971fd52fc694bdb5a0cb5ec08 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Wed, 21 Jul 2021 21:20:56 -0500 Subject: [PATCH 4/5] Update aspnetcore/blazor/components/data-binding.md Co-authored-by: Mackinnon Buck --- aspnetcore/blazor/components/data-binding.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/blazor/components/data-binding.md b/aspnetcore/blazor/components/data-binding.md index 3f271e05bdb0..9367aed9bf99 100644 --- a/aspnetcore/blazor/components/data-binding.md +++ b/aspnetcore/blazor/components/data-binding.md @@ -124,7 +124,7 @@ Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attrib @code { public string[] SelectedCars { get; set; } = new string[] { }; - public string[] SelectedCities { get; set; } = new[] { "\"bal\"", "\"sea\"" }; + public string[] SelectedCities { get; set; } = new[] { "bal", "sea" }; void SelectedCarsChanged(ChangeEventArgs e) { From 12309be4967589e00246843487b7d8679aca6d5a Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Wed, 28 Jul 2021 05:43:05 -0500 Subject: [PATCH 5/5] Note feature for Preview 7 (will be reverted later) --- aspnetcore/blazor/components/data-binding.md | 2 ++ aspnetcore/blazor/forms-validation.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/aspnetcore/blazor/components/data-binding.md b/aspnetcore/blazor/components/data-binding.md index 9367aed9bf99..e73ebbce9584 100644 --- a/aspnetcore/blazor/components/data-binding.md +++ b/aspnetcore/blazor/components/data-binding.md @@ -79,6 +79,8 @@ Razor attribute binding is case sensitive: ## Multiple option selection with `` elements +*This feature applies to ASP.NET Core 6.0 Preview 7 or later. ASP.NET Core 6.0 is scheduled for release later this year.* + Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attributes/multiple) option selection with `` elements. The [`@onchange`](xref:mvc/views/razor#onevent) event provides an array of the selected elements via [event arguments (`ChangeEventArgs`)](xref:blazor/components/event-handling#event-arguments). The value must be bound to an array type. `Pages/BindMultipleInput.razor`: diff --git a/aspnetcore/blazor/forms-validation.md b/aspnetcore/blazor/forms-validation.md index 269905316cd4..587f6b021819 100644 --- a/aspnetcore/blazor/forms-validation.md +++ b/aspnetcore/blazor/forms-validation.md @@ -255,6 +255,8 @@ In the following example: ## Multiple option selection with the `InputSelect` component +*This feature applies to ASP.NET Core 6.0 Preview 7 or later. ASP.NET Core 6.0 is scheduled for release later this year.* + Binding supports [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attributes/multiple) option selection with the component. The [`@onchange`](xref:mvc/views/razor#onevent) event provides an array of the selected options via [event arguments (`ChangeEventArgs`)](xref:blazor/components/event-handling#event-arguments). The value must be bound to an array type, and binding to an array type makes the [`multiple`](https://developer.mozilla.org/docs/Web/HTML/Attributes/multiple) attribute optional on the tag. In the following example, the user must select at least two starship classifications but no more than three classifications.