--- title: ASP.NET Core Blazor data binding author: guardrex description: Learn about data binding features for components and DOM elements in Blazor apps. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc ms.date: 03/26/2020 no-loc: [Blazor, SignalR] uid: blazor/data-binding --- # ASP.NET Core Blazor data binding By [Luke Latham](https://github.com/guardrex) and [Daniel Roth](https://github.com/danroth27) Razor components provide data binding features via an HTML element attribute named [`@bind`](xref:mvc/views/razor#bind) with a field, property, or Razor expression value. The following example binds the `CurrentValue` property to the text box's value: ```razor @code { private string CurrentValue { get; set; } } ``` When the text box loses focus, the property's value is updated. The text box is updated in the UI only when the component is rendered, not in response to changing the property's value. Since components render themselves after event handler code executes, property updates are *usually* reflected in the UI immediately after an event handler is triggered. Using `@bind` with the `CurrentValue` property (``) is essentially equivalent to the following: ```razor CurrentValue = __e.Value.ToString())" /> @code { private string CurrentValue { get; set; } } ``` When the component is rendered, the `value` of the input element comes from the `CurrentValue` property. When the user types in the text box and changes element focus, the `onchange` event is fired and the `CurrentValue` property is set to the changed value. In reality, the code generation is more complex because `@bind` handles cases where type conversions are performed. In principle, `@bind` associates the current value of an expression with a `value` attribute and handles changes using the registered handler. Bind a property or field on other events by also including an `@bind:event` attribute with an `event` parameter. The following example binds the `CurrentValue` property on the `oninput` event: ```razor @code { private string CurrentValue { get; set; } } ``` Unlike `onchange`, which fires when the element loses focus, `oninput` fires when the value of the text box changes. Use `@bind-{ATTRIBUTE}` with `@bind-{ATTRIBUTE}:event` syntax to bind element attributes other than `value`. In the following example, the paragraph's style is updated when the `_paragraphStyle` value changes: ```razor @page "/binding-example"
Blazorify the app!
@code { private string _paragraphStyle = "color:red"; } ``` Attribute binding is case sensitive. For example, `@bind` is valid, and `@Bind` is invalid. ## 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. Consider the following scenario: * An `` element is bound to an `int` type with an initial value of `123`: ```razor @code { [Parameter] public int MyProperty { get; set; } = 123; } ``` * The user updates the value of the element to `123.45` in the page and changes the element focus. In the preceding scenario, the element's value is reverted to `123`. When the value `123.45` is rejected in favor of the original value of `123`, the user understands that their value wasn't accepted. By default, binding applies to the element's `onchange` event (`@bind="{PROPERTY OR FIELD}"`). Use `@bind="{PROPERTY OR FIELD}" @bind:event={EVENT}` to trigger binding on a different event. For the `oninput` event (`@bind:event="oninput"`), the reversion occurs after any keystroke that introduces an unparsable value. When targeting the `oninput` event with an `int`-bound type, a user is prevented from typing a `.` character. A `.` character is immediately removed, so the user receives immediate feedback that only whole numbers are permitted. There are scenarios where reverting the value on the `oninput` event isn't ideal, such as when the user should be allowed to clear an unparsable `` value. Alternatives include: * Don't use the `oninput` event. Use the default `onchange` event (only specify `@bind="{PROPERTY OR FIELD}"`), where an invalid value isn't reverted until the element loses focus. * Bind to a nullable type, such as `int?` or `string`, and provide custom logic to handle invalid entries. * Use a [form validation component](xref:blazor/forms-validation), such as `InputNumber` or `InputDate`. Form validation components have built-in support to manage invalid inputs. Form validation components: * Permit the user to provide invalid input and receive validation errors on the associated `EditContext`. * Display validation errors in the UI without interfering with the user entering additional webform data. ## Format strings Data binding works withYear: @Year
@code { [Parameter] public int Year { get; set; } [Parameter] public EventCallbackParentYear: @ParentYear
ParentYear: 1978
Year: 1978
``` If the value of the `ParentYear` property is changed by selecting the button in the `ParentComponent`, the `Year` property of the `ChildComponent` is updated. The new value of `Year` is rendered in the UI when the `ParentComponent` is rerendered: ```htmlParentYear: 1986
Year: 1986
``` The `Year` parameter is bindable because it has a companion `YearChanged` event that matches the type of the `Year` parameter. By convention, `