I've written a bunch of HTML Helpers that cover basically all HTML elements and some HTML structures like tabs, pagers, trees etc. Now, I'd like to convert these into TagHelpers. But I couldn't find a way how to do one essential thing.
In order to generate a table using my HTML helper, you need to write something like this:
@(Html.Table(Model.Items).Columns(columns =>
{
columns.BoundColumn(m => m.Id).Class("id");
columns.TemplateColumn().Class("switch")
.Pattern((model, pattern) => pattern.Html(@<span>@(model.Title)</span>));
columns.BoolColumn(m => m.Default);
})
It takes a collection as a constructor parameter and makes the items type available for the Columns property so that it's possible to bind the item properties to defined columns using lambda/anonymous functions. I have several columns that renders text/html based on its type - BoundColumn renders text, BoolColumn takes bool as a value and renders Yes/No, LinkColumn renders a link. And there's also a TemplateColumn that takes IHtmlString and this way you can pass any html structure you want to display for a particular column.
So I was hoping that TagHelper would look something like this:
<tee-table data-source="Model.Items">
<columns>
<bound value="Id" />
<bool value="Default" />
<template value="SomeComplexType">
<span>@value.Title</span>
</template>
</columns>
</tee-table>
But it looks like TagHelpers don't support this at all. I found this article: http://www.dotnet-programming.com/post/2015/10/31/Building-Complex-Controls-with-AspNet-MVC-6-TagHelpers.aspx that is somehow close to what I'd like to do but not close enough. It still requires iteration in the view, which is something I want to avoid. Mainly because Tag Helpers are supposed to support the "HTML feel" in MVC views.
So is there a way how to build a TagHelper that would work as a template for collection items?
Currently (as of 3/21/2017) TagHelpers do not have a great way to deal with this. However, there is an ongoing issue to add templating support to TagHelpers here.