Skip to content

Commit 0305c3a

Browse files
authored
Update Blazor binding content (#20267)
1 parent 6c34435 commit 0305c3a

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

aspnetcore/blazor/components/data-binding.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about data binding features for components and DOM elements i
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 08/19/2020
8+
ms.date: 10/22/2020
99
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/components/data-binding
1111
---
@@ -125,10 +125,16 @@ Specifying a format for the `date` field type isn't recommended because Blazor h
125125
<input type="date" @bind="startDate" @bind:format="yyyy-MM-dd">
126126
```
127127

128-
## Parent-to-child binding with component parameters
128+
## Binding with component parameters
129+
130+
A common scenario is binding a property in a child component to a property in its parent. This scenario is called a *chained bind* because multiple levels of binding occur simultaneously.
129131

130132
Component parameters permit binding properties and fields of a parent component with `@bind-{PROPERTY OR FIELD}` syntax.
131133

134+
Chained binds can't be implemented with [`@bind`](xref:mvc/views/razor#bind) syntax in the child component. An event handler and value must be specified separately to support updating the property in the parent from the child component.
135+
136+
The parent component still leverages the [`@bind`](xref:mvc/views/razor#bind) syntax to set up the data-binding with the child component.
137+
132138
The following `Child` component (`Shared/Child.razor`) has a `Year` component parameter and `YearChanged` callback:
133139

134140
```razor
@@ -139,16 +145,25 @@ The following `Child` component (`Shared/Child.razor`) has a `Year` component pa
139145
</div>
140146
</div>
141147
148+
<button @onclick="UpdateYearFromChild">Update Year from Child</button>
149+
142150
@code {
151+
private Random r = new Random();
152+
143153
[Parameter]
144154
public int Year { get; set; }
145155
146156
[Parameter]
147157
public EventCallback<int> YearChanged { get; set; }
158+
159+
private async Task UpdateYearFromChild()
160+
{
161+
await YearChanged.InvokeAsync(r.Next(1950, 2021));
162+
}
148163
}
149164
```
150165

151-
The callback (<xref:Microsoft.AspNetCore.Components.EventCallback%601>) must be named as the component parameter name followed by the "`Changed`" suffix (`{PARAMETER NAME}Changed`). In the preceding example, the callback is named `YearChanged`. For more information on <xref:Microsoft.AspNetCore.Components.EventCallback%601>, see <xref:blazor/components/event-handling#eventcallback>.
166+
The callback (<xref:Microsoft.AspNetCore.Components.EventCallback%601>) must be named as the component parameter name followed by the "`Changed`" suffix (`{PARAMETER NAME}Changed`). In the preceding example, the callback is named `YearChanged`. <xref:Microsoft.AspNetCore.Components.EventCallback.InvokeAsync%2A?displayProperty=nameWithType> invokes the delegate associated with the binding with the provided argument and dispatches an event notification for the changed property.
152167

153168
In the following `Parent` component (`Parent.razor`), the `year` field is bound to the `Year` parameter of the child component:
154169

@@ -182,13 +197,7 @@ By convention, a property can be bound to a corresponding event handler by inclu
182197
<Child @bind-Year="year" @bind-Year:event="YearChanged" />
183198
```
184199

185-
## Child-to-parent binding with chained bind
186-
187-
A common scenario is chaining a data-bound parameter to a page element in the component's output. This scenario is called a *chained bind* because multiple levels of binding occur simultaneously.
188-
189-
A chained bind can't be implemented with [`@bind`](xref:mvc/views/razor#bind) syntax in the child component. The event handler and value must be specified separately. A parent component, however, can use [`@bind`](xref:mvc/views/razor#bind) syntax with the child component's parameter.
190-
191-
The following `PasswordField` component (`PasswordField.razor`):
200+
In a more sophisticated and real-world example, the following `PasswordField` component (`PasswordField.razor`):
192201

193202
* Sets an `<input>` element's value to a `password` field.
194203
* Exposes changes of a `Password` property to a parent component with an [`EventCallback`](xref:blazor/components/event-handling#eventcallback) that passes in the current value of the child's `password` field as its argument.
@@ -300,6 +309,8 @@ Password:
300309
}
301310
```
302311

312+
For more information on <xref:Microsoft.AspNetCore.Components.EventCallback%601>, see <xref:blazor/components/event-handling#eventcallback>.
313+
303314
## Bind across more than two components
304315

305316
You can bind through any number of nested components, but you must respect the one-way flow of data:

0 commit comments

Comments
 (0)