Pass the dashboard’s stock count to a badge.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
<StockBadge Units="{Available}" />Save the markup above as Dashboard.html.
The example is the full program below: unzip it and dotnet run.
The badge initially displays In stock: 3 and updates to In stock: 1 when the parent changes.
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 Dashboard.html to Dashboard.cs and implements the partial properties. The example’s project file pins the package with:
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />Dashboard.cs:
using OneUI.Components; public partial class Dashboard { [State] public partial int Available { get; set; } }
StockBadge.html:
<p>In stock: {Units}</p>StockBadge.cs:
using OneUI.Components; public partial class StockBadge { [Param(Required = true)] public partial int Units { get; set; } }
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 dashboard = new Dashboard { Available = 3 }; ui.Mount(dashboard); Console.WriteLine(dom.ToHtml()); dashboard.Available = 1; Console.WriteLine(dom.ToHtml());
dotnet run prints:
<p>In stock: 3</p> <p>In stock: 1</p>
Omit Units from <StockBadge /> and the compiler reports OUI0061: the required parameter is missing. See markup diagnostics.
Variations
Pass a fixed number. In Dashboard.html, replace the expression with a literal:
<StockBadge Units="3" />The literal converts to int. The badge prints In stock: 3 both times because it no longer reads Available.
Related recipes
The short answer
Declare a public partial [Param] property on the child and pass a value with an attribute on the child tag. In the example, Units="{Available}" reads Available on the parent and sets Units on the child. The child template reads {Units}.
How do you pass data between parent and child components?
Use the child’s class name, with its capital letter, as the tag name. Use the child property’s name as the attribute name and a C# expression from the parent as its value. Available and Units don’t have to share a name. The left side of the attribute is the child’s input; the expression on the right is evaluated in the parent.
Make the child responsible for how it presents the value, and let the parent own the underlying data. The parent here can change the stock count without knowing whether the badge renders a paragraph, a span, or something else. For a child action that should change parent state, OneUI supports Action parameters: pass a parent callback and have the child call it rather than treating a display parameter as shared writable state.
How do you make a component parameter required?
Use [Param(Required = true)] when the child can’t do its job without the value. The check is at the call site in the parent template, so a missing attribute is diagnosed before you run the app. The compiler checks the parameter names and types, too. If you want an optional value instead, declare [Param] without Required and set the default in the child’s constructor.
For a fixed count, a literal like Units="3" is enough. For a changing count, keep the braces: Units="{Available}" is a binding, not a request to convert the word “Available” to an integer. Updating the generated Available property causes the bound parameter to be reevaluated. The child’s binding then updates its text from the new Units value.