I have an MVC5 app which I'm porting to .NET 6. Previously, we used an HTML helper which allowed us to pass some HTML and return a button type thing with the passed HTML used as part of the rendered output. Ie:
@helper InfoRow(string title, Func<dynamic, HelperResult> content)
{
<div class="row">
<label class="col-md-3 col-form-label">@title</label>
<div class="col">
<span class="form-control-plaintext">
@content(null)
</span>
</div>
</div>
}
Called using
@InfoRow("Notes", @<span><b>@row.Number</b> new notes</span>)
I've ben changing helpers to functions in .NET 6 but am a little stuck on this one as I can't find any examples of passing HTML t a function. Is it possible?
So far I have this:
@functions {
private void InfoRow(string title, IHtmlContent content)
{
<div class="row">
<label class="col-md-3 col-form-label">@title</label>
<div class="col">
<span class="form-control-plaintext">
@content
</span>
</div>
</div>
}
}
I have created this working example. I hope it helps you:
@using Microsoft.AspNetCore.Html;
@using Microsoft.AspNetCore.Mvc.Razor;
@{
ViewData["Title"] = "Home Page";
}
@functions{
void InfoRow(string title, Func<dynamic, HelperResult> content)
{
<div class="row">
<label class="col-md-3 col-form-label">@title</label>
<div class="col">
<span class="form-control-plaintext">
@content(string.Empty)
</span>
</div>
</div>
}
}
@{
InfoRow("Test title",@<span> @ViewData["Title"]</span>);
}