Skip to content

Commit bd88edf

Browse files
authored
Improve binding guidance (#34323)
1 parent 4aa7c14 commit bd88edf

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

aspnetcore/blazor/components/data-binding.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ Additional examples
221221

222222
For more information on the `InputText` component, see <xref:blazor/forms/input-components>.
223223

224-
Components support two-way data binding by defining a pair of parameters:
224+
Components support two-way data binding by defining a pair of `@bind` attributes with either a `:get` or `:set` modifier . The `{PARAMETER}` placeholder in the following examples is used to bind a component parameter:
225225

226-
* `@bind:get`: Specifies the value to bind.
227-
* `@bind:set`: Specifies a callback for when the value changes.
226+
* `@bind:get`/`@bind-{PARAMETER}:get`: Specifies the value to bind.
227+
* `@bind:set`/`@bind-{PARAMETER}:set`: Specifies a callback for when the value changes.
228228

229-
The `@bind:get` and `@bind:set` modifiers are always used together.
229+
The `:get` and `:set` modifiers are always used together.
230230

231-
Examples
231+
With `:get`/`:set` binding, you can react to a value change before it's applied to the DOM, and you can change the applied value, if necessary. Whereas with `@bind:event="{EVENT}"` attribute binding, where the `{EVENT}` placeholder is a DOM event, you receive the notification after the DOM is updated, and there's no capacity to modify the applied value while binding.
232232

233233
`BindGetSet.razor`:
234234

@@ -378,14 +378,16 @@ Using `@bind:get`/`@bind:set` modifiers both controls the underlying value of `i
378378
:::moniker range=">= aspnetcore-7.0"
379379

380380
> [!NOTE]
381-
> Two-way binding to a property with `get`/`set` accessors requires discarding the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType>. For two-way data binding, we recommend using `@bind:get`/`@bind:set` modifiers. For more information, see the `@bind:get`/`@bind:set` guidance in the earlier in this article.
381+
> Across multiple components, two-way binding to a property with `get`/`set` accessors requires discarding the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType> in the property's setter. For two-way data binding, we recommend using `@bind:get`/`@bind:set` modifiers. For more information, see the [`@bind:get`/`@bind:set` guidance](#binding-features) earlier in this article.
382+
>
383+
> To see an example of how the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType> is discarded in .NET 6 or earlier before `@bind:get`/`@bind:set` modifiers became a framework feature, see [the `NestedChild` component of the *Bind across more than two components* section in the .NET 6 version of this article](?view=aspnetcore-6.0&preserve-view=true#bind-across-more-than-two-components).
382384
383385
:::moniker-end
384386

385387
:::moniker range="< aspnetcore-7.0"
386388

387389
> [!NOTE]
388-
> Two-way binding to a property with `get`/`set` accessors requires discarding the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType>. For two-way data binding in ASP.NET Core in .NET 7 or later, we recommend using `@bind:get`/`@bind:set` modifiers, which are described in 7.0 or later versions of this article.
390+
> Two-way binding to a property with `get`/`set` accessors requires discarding the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType>. For an example, see [the `NestedChild` component of the *Bind across more than two components* section](#bind-across-more-than-two-components). For two-way data binding in .NET 7 or later, we recommend using `@bind:get`/`@bind:set` modifiers, which are described in 7.0 or later versions of this article.
389391
390392
:::moniker-end
391393

@@ -889,6 +891,8 @@ In the following `NestedChild` component, the `NestedGrandchild` component:
889891
* Assigns the value of `ChildMessage` to `GrandchildMessage` with `@bind:get` syntax.
890892
* Updates `GrandchildMessage` when `ChildMessageChanged` executes with `@bind:set` syntax.
891893

894+
Prior to the release of .NET 7, two-way binding across components uses `get`/`set` accessors with a third property that discards the <xref:System.Threading.Tasks.Task> returned by <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType> in its setter. To see an example of this approach for .NET 6 or earlier before `@bind:get`/`@bind:set` modifiers became a framework feature, see [the `NestedChild` component of this section in the .NET 6 version of this article](?view=aspnetcore-6.0&preserve-view=true#bind-across-more-than-two-components).
895+
892896
:::moniker-end
893897

894898
`NestedChild.razor`:

0 commit comments

Comments
 (0)