Reputation: 718
I'm using a component from a third-party package, that accepts a RenderFragment as a parameter, and I want to assign a value to that RenderFragment through a Partial Class of my Index Page.
I realized that when I construct the RenderFragment in the code
tags, it works. But once you put it in a partial class
, Visual Studio starts flagging errors.
Example Code that works:
Razor File:
<Statistic Title="Feedback" Value="1128" PrefixTemplate="@prefix1" />
@code {
public RenderFragment prefix1 = @(<Icon Type = "like" />);
}
Example code that doesn't work:
Razor File:
<Statistic Title="Feedback" Value="1128" PrefixTemplate="@prefix1" />
Partial Class:
public partial class Index {
RenderFragment prefix1 = @(<Icon Type = "like" />);
}
So my question now is, how do I construct a render fragment in a partial class? I dont seem to find anything around this subject online.
Thanks in advance.
Upvotes: 6
Views: 5916
Reputation: 718
I settled for the RenderTreeBuilder, as Mister Magoo suggested, and since I'd like to keep my business logic away from the UI.
This is how I did it:
Razor File:
<Statistic Title="Feedback" Value="1128" PrefixTemplate="@prefix1" />
Partial Class:
public partial class OperationCard
{
RenderFragment prefix1 = builder =>
{
builder.OpenComponent<Icon>(1);
builder.AddAttribute(2, "Type", "like");
builder.CloseComponent();
};
}
Upvotes: 9