HtmlTableHelper
Mini C# IEnumerable object to HTML Table String Library
Install / Use
/learn @mini-software/HtmlTableHelperREADME
<div style="text-align: center"><p><a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=en"><img src="https://img.shields.io/badge/EN-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=zh-CN"><img src="https://img.shields.io/badge/简中-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=zh-TW"><img src="https://img.shields.io/badge/繁中-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=ja"><img src="https://img.shields.io/badge/日本語-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=ko"><img src="https://img.shields.io/badge/한국어-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=th"><img src="https://img.shields.io/badge/ไทย-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=fr"><img src="https://img.shields.io/badge/Français-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=de"><img src="https://img.shields.io/badge/Deutsch-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=es"><img src="https://img.shields.io/badge/Español-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=it"><img src="https://img.shields.io/badge/Italiano-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=ru"><img src="https://img.shields.io/badge/Русский-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=pt"><img src="https://img.shields.io/badge/Português-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=nl"><img src="https://img.shields.io/badge/Nederlands-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=pl"><img src="https://img.shields.io/badge/Polski-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=ar"><img src="https://img.shields.io/badge/العربية-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=tr"><img src="https://img.shields.io/badge/Türkçe-white" alt="version"></a> <a href="https://openaitx.github.io/view.html?user=mini-software&project=HtmlTableHelper&lang=vi"><img src="https://img.shields.io/badge/Tiếng Việt-white" alt="version"></a> </p></div>
Features
- Mini (DLL Size Only 20KB) and Easy to use.
- Support .NET Standard 2.0/.NET 4.6/.NET 4.5/.NET 4.0
- Without Any Third Party Library
- Support Anonymous Types,Dapper Dynamic Query,List/Array/Set/Enumrable,DataTable,Dictionary
Installation
You can install the package from NuGet using the Visual Studio Package Manager or NuGet UI:
PM> install-package HtmlTableHelper
or the dotnet command line:
dotnet add package HtmlTableHelper
Fiddle Demo:
Get Start
List/Array/Set/Enumrable non Key/Value Type Example
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
Result:
<table><thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead><tbody><tr><td>ITWeiHan</td><td>25</td><td>M</td></tr></tbody></table>
*/
Dapper Example
using (var cn = "Your Connection")
{
var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");
var tablehtml = sourceData.ToHtmlTable();
}
Dictionary Example
var sourceData = new[] {new Dictionary<string, object> (){{"Name" , "ITWeiHan" },{"Age",25},{"Gender","M"}}};
var tablehtml = sourceData.ToHtmlTable();
<!--
**But** anothor Key/Value Type please use `ToHtmlTableByDictionary`
```C#
var sourceData = new[] {
new Dictionary<SomeKeyType, SomeValueType> (){...}
};
var tablehtml = sourceData.ToHtmlTableByDictionary();
```
--->
Custom Table/TR/TD/TH Attributes (Dynamic Type)
var data = /*List/Array/Set/Enumrable..*/;
var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //this is dynamic type, support all attribute
,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" }
);
/*
Result:
<table class="SomeClass">
<thead>
<tr ID="SomeID">
<th class="dark-theme">..</th>
</tr>
</thead>
<tbody>
<tr ID="SomeID">
<td width="120 px">..</td>
</tr>
</tbody>
</table>
*/
Attribute Annotation
1. Display :
public class ModelClassWithDisplayAttr
{
[TableColumn(DisplayName = "Column1")] //MyProperty1 property will render thead-td's innertext : "Column1"
public string MyProperty1 { get; set; }
[TableColumn(DisplayName = "Column2")] //MyProperty2 property will render thead-td's innertext : "Column2"
public string MyProperty2 { get; set; }
}
2. Skip :
public class ModelClassWithSkipAttr
{
[TableColumn( Skip = true)]
public string MyProperty1 { get; set; } //MyProperty1 will not render html
public string MyProperty2 { get; set; }
}
HTMLTableBuilder
HtmlCaption
var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} };
var html = soucreData.CreateBuilder()
.SetCaption("This is Caption", new { id = "CaptionId" })
.ToHtmlTable();
//Result : <table><caption id=\"CaptionId\" >This is Caption</caption><thead><tr><th>MyProperty1</th><th>MyProperty2</th></tr></thead><tbody><tr><td>test</td><td>123</td></tr></tbody></table>
HTMLTableSetting
Configurable InnerHtml Encoding (Recommended not to do so without a specific reason,because XSS Attack)
var sourceData = new[] { new { Name = "<b>ITWeiHan</b>" } };
//Default Encoding
var encodinghtml = sourceData.ToHtmlTable();
//Result: <table>..<b>ITWeiHan</b>..</table>
var htmltablesetting = new HTMLTableSetting()
{
IsHtmlEncodeMode = false
};
var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting);
//Result: <table>..<b>ITWeiHan</b>..</table>
Extension
ASP.NET Core MVC:
Create a IHtmlHelperExtension.cs
using System.Collections.Generic;
using HtmlTableHelper;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;
public static class IHtmlHelperExtension
{
public static HtmlString ToHtmlTable<T>(this IHtmlHelper htmlHelper, IEnumerable<T> enums
, object tableAttributes = null, object trAttributes = null, object tdAttributes = null
, HtmlTableSetting HTMLTableSetting = null)
{
var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable<T>(this IHtmlHelper htmlHelper, System.Data.DataTable datatable
, object tableAttributes = null, object trAttributes = null, object tdAttributes = null
, HtmlTableSetting HTMLTableSetting = null)
{
var html = datatable.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
}
razor.cshtml
@Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } })
/*
Result:<table><thead><tr><th>Name</th><th>Age</th><th>Gender</th></tr></thead><tbody><tr><td>ITWeiHan</td><td>25</td><td>M</td></tr></tbody></table>
*/
ASP.NET MVC 5:
Create a HtmlHelperExtension.cs
using System.Collections.Generic;
using HtmlTableHelper;
using System.Web;
using System.Web.Mvc;
public static class HtmlHelperExtension
{
public static HtmlString ToHtmlTable<T>(this HtmlHelper htmlHelper, IEnumerable<T> enums
, object tableAttributes = null, object trAttributes = null, object tdAttributes = null
, HtmlTableSetting HTMLTableSetting = null)
{
var html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable<T>(this HtmlHelper htmlHelper, System.Data.DataTable datatable
, object tableAttributes = null, object trAttributes = null, object tdAttributes = null
, HtmlTableSetting HTMLTableSetting = null)
{
var html = datatable.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
}
Demo
ASP.NET MVC 5 JQuery DataTable Demo:
using HtmlTableHelper;
//..
public class HomeController : Controller
{
public ActionResult Index()
{
var datas = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
ViewBag.Table = datas.ToHtmlTable();
return View();
}
}
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AspNetMvcDemo</title>
<link href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
</head>
<body>
<div>
@Html.Raw(ViewBag.Table)
</div>
<script src="https://code.jquery.com/jquery-3.3.1.m
Related Skills
node-connect
341.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
84.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
341.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
84.4kCommit, push, and open a PR
