I want to simplify the following
@if (Template != null)
{
@Template
}
else
{
<span>No contents!</span>
}
with either ??
or ?.
Is it possible?
Attempt
My attempts below
@Template?.BeginInvoke()
@{Template==null? @Template : <span> No data to display! </span>}
@(Template??<span> No data to display! </span>)
produce red squiggly lines.
Edit
I think I need to submit the real scenario that I want to simplify.
@typeparam T
@if (Items == null)
{
if (NullTemplate != null)
{
@NullTemplate
}
else
{
<span style="color: red">Null...</span>
}
}
else if (Items.Count == 0)
{
if (EmptyTemplate != null)
{
@EmptyTemplate
}
else
{
<span style="color: red">Empty ...</span>
}
}
else
{
@HeaderTemplate
foreach (T item in Items)
{
@ItemTemplate(item)
}
}
@code{
[Parameter] public RenderFragment NullTemplate { get; set; }
[Parameter] public RenderFragment EmptyTemplate { get; set; }
[Parameter] public RenderFragment HeaderTemplate { get; set; }
[Parameter] public RenderFragment<T> ItemTemplate { get; set; }
[Parameter] public List<T> Items { get; set; }
}
I don't want to buffer the incoming values to ****Template
properties with private fields, pre-process the fields before rendering them (the fields) to HTML. In other words, no additional code in @code{}
directive are allowed.
ComponentBase
because generating HTML withBuildRenderTree
is really cumbersome! – Packton