dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
<ul> <template each="{Tasks}" item="task" key="{task.Id}"> <li>{task.Title}</li> </template> </ul>
Save the markup above as TaskList.html.
The example is the full program below: unzip it and dotnet run.
The component renders Draft, then Review; reversing the collection reverses the two rendered rows.
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 TaskList.html to TaskList.cs and implements the partial properties. The example’s project file pins the package with:
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />TaskList.cs:
using OneUI.Components; public partial class TaskList { [State] public partial TaskRow[] Tasks { get; set; } public TaskList() => Tasks = []; } public sealed record TaskRow(int Id, string Title);
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 list = new TaskList { Tasks = [new(12, "Draft"), new(19, "Review")] }; ui.Mount(list); Console.WriteLine(dom.ToHtml()); list.Tasks = [list.Tasks[1], list.Tasks[0]]; Console.WriteLine(dom.ToHtml());
dotnet run prints:
<ul><li>Draft</li><li>Review</li></ul> <ul><li>Review</li><li>Draft</li></ul>
Leave out key and the template build reports OUI0051. See markup diagnostics.
Variations
Display the row position. In TaskList.html, add index="i" and use the zero-based position in the row:
<ul> <template each="{Tasks}" item="task" index="i" key="{task.Id}"> <li>{i + 1}. {task.Title}</li> </template> </ul>
The labels start as 1. Draft and 2. Review; after the reorder, Review is numbered first.
Related recipes
- Pass parameters to a child HTML component in C#
- Conditionally render HTML in a C# component template
The short answer
Use <template each="{Tasks}" item="task" key="{task.Id}"> inside a list and bind the current item’s properties in the row. Tasks is a collection on the component, task is the name for one item, and task.Id identifies that row across updates.
Why do list items need a unique key?
The key matches a row in the current collection to the same row from the previous update. Here Draft keeps its identity when it moves from the first position to the second, because its ID is still 12. OneUI keeps the row’s existing nodes while its key stays. It can move those nodes rather than create a new set of elements for every position.
Give each item a key that’s unique within this list and stable across edits. A database ID or an ID assigned when the item is created works. The list position isn’t a good key for items that can move: when you insert a row at the top, all subsequent positions change even though the items are the same. A title isn’t a good key if two rows can share it or a user can rename it. Use the optional index to show a position, not to identify a task.
How do you update a rendered list when the collection changes?
The example assigns a new array to the generated Tasks property. That signals that the template should read the collection again. Keeping the same item IDs means that reordering the array also reorders the existing rows. To edit a row without changing its identity, replace that item with one that has the same ID and the updated data, then assign the new collection.
If you use a mutable collection instead, adding an item to it doesn’t call the generated property setter. OneUI generates NotifyTasksChanged() for that case. Call it after mutating Tasks in place, or assign a new collection. In either case the row markup still comes from the template, and the key still describes which item each row represents.