---
title: "Pass parameters to a child HTML component in C#"
url: https://recursiveai.org/oneui/cookbook/pass-parameters-to-child-component/
description: "Pass a typed parameter from a parent template to a child HTML component with OneUI. See the child update when its parent’s C# state changes."
version: "1.0.0"
---

# Pass parameters to a child HTML component in C#

**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/pass-parameters-to-child-component/pass-parameters-to-child-component.zip)

Pass the dashboard’s stock count to a badge.

```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
<StockBadge Units="{Available}" />
```

Save the markup above as `Dashboard.html`.

[The example](https://recursiveai.org/oneui/cookbook/pass-parameters-to-child-component/pass-parameters-to-child-component.zip) 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:

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

`Dashboard.cs`:

```csharp
using OneUI.Components;

public partial class Dashboard
{
    [State]
    public partial int Available { get; set; }
}
```

`StockBadge.html`:

```html
<p>In stock: {Units}</p>
```

`StockBadge.cs`:

```csharp
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.

```csharp
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:

```text
<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](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

**Pass a fixed number.** In `Dashboard.html`, replace the expression with a literal:

```html
<StockBadge Units="3" />
```

The literal converts to `int`. The badge prints `In stock: 3` both times because it no longer reads `Available`.

## Related recipes

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

## 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.
