I am trying to pass a ListItem component to a List component to be displayed.
The ListItem Component looks like this: Razor:
<li>
@Item
</li>
C#
using Microsoft.AspNetCore.Components;
namespace Components
{
public partial class TextListItem
{
[Parameter]
public new string Item { get; set; }
}
}
This should just show a list item with text in it.
The List Component looks like this: Razor:
@typeparam T
<ul>
@foreach(T item in Items)
{
@ChildContent(item)
}
</ul>
using Microsoft.AspNetCore.Components;
namespace Components
{
public partial class ListComponent<T>
{
[Parameter]
public List<T> Items { get; set; }
[Parameter]
public RenderFragment<T> ChildContent { get; set; }
}
}
This should just fill the unordered list with list items.
And then in Index I have something like:
<ListComponent Items = "@textList" >
@context.Item
</ListComponent>
@code{
private List<TextListItem> textList;
protected override void OnInitialized()
{
base.OnInitialized();
textList = new List<TextListItem>()
{
new TextListItem(){Item ="one" },
new TextListItem(){Item ="two" },
new TextListItem(){Item ="three" },
};
}
}
I don't get back HTML from the list textList, just the text in Item. I am making this complex because I want to get various fragment of HTML back for different types of lists. Why don't I get the HTML back? I think I am supposed to. Thanks!