---
title: "Two-way bind an HTML input to C# state"
url: https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/
description: "Two-way bind an HTML input to C# state with OneUI’s bind:value. See user edits update the property and a C# assignment update the field."
version: "1.0.0"
---

# Two-way bind an HTML input to C# state

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in desktop windows or in a browser via WebAssembly with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/two-way-bind-input-value.zip)

Edit a name and update its live preview.

```shell
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```html
<label>
  Name <input bind:value="Name" />
</label>
<p>Preview: {Name}</p>
```

Save the markup above as `NameEditor.html`.

[The example](https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/two-way-bind-input-value.zip) is the full program below: unzip it and `dotnet run`.

An input event changes `Name` to `Grace`. Assigning `Ada` in C# then restores the original field value.

## The full program

Create a `dotnet new console` project and add the package as shown above, or use the download with all the files in place. The NuGet package includes the generator: it matches `NameEditor.html` to `NameEditor.cs` and implements the partial properties. The example’s project file pins the package with:

```xml
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />
```

`NameEditor.cs`:

```csharp
using OneUI.Components;

public partial class NameEditor
{
    [State]
    public partial string Name { get; set; }

    public NameEditor() => Name = "Ada";
}
```

Replace `Program.cs` with the code below. It mounts the component on a `ReferenceDocument`, an in-memory HTML tree, and prints the result without opening a window.

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var dom = new ReferenceDocument();
var ui = new UiContext(dom);
dom.Context = ui;
var editor = new NameEditor();
ui.Mount(editor);
Console.WriteLine(dom.ToHtml());

dom.Input("input", "Grace");
Console.WriteLine($"C# state: {editor.Name}");
Console.WriteLine(dom.ToHtml());

editor.Name = "Ada";
Console.WriteLine(dom.ToHtml());
```

`dotnet run` prints:

```text
<label>Name <input .value="Ada"></input></label><p>Preview: Ada</p>
C# state: Grace
<label>Name <input .value="Grace"></input></label><p>Preview: Grace</p>
<label>Name <input .value="Ada"></input></label><p>Preview: Ada</p>
```

In the HTML dump, `.value` is the reference document’s notation for the live value property, not an attribute you should copy into a template.

To mount `NameEditor` in a browser instead of using `ReferenceDocument`, register it as a custom element with `BrowserApp.Define` in a [OneApp WebAssembly app](https://recursiveai.org/oneapp/cookbook/export-csharp-component-as-custom-element/).

If the binding target can’t be assigned, the compiler reports `OUI0041`; bind to a settable member. See [markup diagnostics](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

**Bind a multiline field.** Replace the label and input in `NameEditor.html` with a label and `textarea`; keep the preview:

```html
<label>
  Name <textarea bind:value="Name"></textarea>
</label>
```

In `Program.cs`, change the selector in `dom.Input` to `"textarea"`. The input still writes `Grace` to the same C# property.

## Related recipes

- [Build a reactive HTML component with C# state](https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/)
- [Conditionally render HTML in a C# component template](https://recursiveai.org/oneui/cookbook/render-conditional-content-in-template/)

## The short answer

Write `bind:value="Name"` on the input and declare `Name` as a settable `[State]` property. OneUI uses `Name` to set the field’s value and writes incoming `input` event values back to `Name`. The preview reads the same property with `{Name}`.

## What is bidirectional data binding?

Bidirectional, or two-way, binding connects a field to state in both directions. When the user edits the field, the bound state changes, and any other binding that reads it can update. When C# sets the state, OneUI updates the field without requiring an input event. Notice that the last line of the example output shows the value and the preview both back at `Ada`; the program didn’t simulate typing `Ada` a second time.

A plain `value="{Name}"` attribute binding expresses a value derived from `Name`; it doesn’t install the write-back behavior of `bind:value`. Use the latter when you want to edit the state through that field. The binding target is written as an assignable expression, without the `{…}` used for text interpolation.

## Does two-way binding update as you type or when you leave the input?

`bind:value` listens for `input` events on `input`, `textarea`, and `select`. For a text box, the preview can change as the value is edited, without waiting for a blur or a form submission. The reference document’s `Input` method sends that event without a browser or native window.

For an on/off field, bind the checked state rather than the field’s text value. OneUI uses `bind:checked` with checkbox and radio inputs and writes back on `change`. These bindings synchronize field values and state; validation and saving are separate decisions. For example, a preview can reflect each edit while your app waits for an explicit confirmation before storing the final value.
