I have a simple Blazor component which iterates over a Generic List :
@typeparam TItem;
@foreach (var item in List)
{
@ElementTemplate(item); // What code should be here to pass i to the ElementTemplate????
i++;
}
@code {
int i = 0;
[Parameter] public List<TItem> List { get; set; }
[Parameter] public RenderFragment<TItem> ElementTemplate { get; set; }
}
I have another simple component which will get an item and an index to render data(Employee):
<div> @Index . @Person.Name </div>
@code{
[Parameter] public Person { get; set; }
[Parameter] public int Index { get; set; }
}
And in my main page I have the following:
<GenericList List="employees">
<ElementTemplate>
<Employee Person="context" Index="?"></Employee>
</ElementTemplate>
</GenericList>
As you can see Employee component needs an Index parameter, how can I pass the Index from the GenericList component? In this example the variable 'i' should be passed to ElementTemplate as well as the Generic TItem object itself.