Edit a name and update its live preview.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
<label> Name <input bind:value="Name" /> </label> <p>Preview: {Name}</p>
Save the markup above as NameEditor.html.
The example 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:
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />NameEditor.cs:
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.
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:
<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.
If the binding target can’t be assigned, the compiler reports OUI0041; bind to a settable member. See markup diagnostics.
Variations
Bind a multiline field. Replace the label and input in NameEditor.html with a label and textarea; keep the preview:
<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
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.