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/Enumerable, DataTable, Dictionary
Installation
You can install the package from NuGet using the Visual Studio Package Manager or NuGet UI:
PM> install-package HtmlTableHelperor the dotnet command line:
dotnet add package HtmlTableHelperFiddle Demo:
Get Start
##### List/Array/Set/Enumerable non Key/Value Type Example
``C#
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
Result:
*/
Name Age Gender ITWeiHan 25 M
C#
using (var cn = "Your Connection")
{
var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");
var tablehtml = sourceData.ToHtmlTable();
}
##### Dapper Example
C#
var sourceData = new[] {new Dictionary
##### Dictionary Example
Custom Table/TR/TD/TH Attributes (Dynamic Type) C# var data = /List/Array/Set/Enumerable../; 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:
| .. |
|---|
| .. |
##### Attribute Annotation###### 1. Display :
C#
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 : C#
public class ModelClassWithSkipAttr
{
[TableColumn( Skip = true)]
public string MyProperty1 { get; set; } //MyProperty1 will not render html
public string MyProperty2 { get; set; }
}
##### HTMLTableBuilder###### HtmlCaption
C#
var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} };
var html = soucreData.CreateBuilder()
.SetCaption("This is Caption", new { id = "CaptionId" })
.ToHtmlTable();
//Result : | MyProperty1 | MyProperty2 |
|---|---|
| test | 123 |
##### HTMLTableSettingConfigurable InnerHtml Encoding (Recommended not to do so without a specific reason, because XSS Attack)
C#
var sourceData = new[] { new { Name = "ITWeiHan" } };//Default Encoding var encodinghtml = sourceData.ToHtmlTable(); //Result:
var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //Result:
Extension
ASP.NET Core MVC:
Create a IHtmlHelperExtension.csC#
using System.Collections.Generic;
using HtmlTableHelper;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;public static class IHtmlHelperExtension
{
public static HtmlString ToHtmlTable
public static HtmlString ToHtmlTable
C#
@Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } })
/*
Result:
razor.cshtml
*/
Name Age Gender ITWeiHan 25 M
C#
using System.Collections.Generic;
using HtmlTableHelper;
using System.Web;
using System.Web.Mvc;
ASP.NET MVC 5:
Create a HtmlHelperExtension.cs
public static class HtmlHelperExtension
{
public static HtmlString ToHtmlTablevar html = enums.ToHtmlTable(tableAttributes, trAttributes, tdAttributes, HTMLTableSetting);
return new HtmlString(html);
}
public static HtmlString ToHtmlTable
Demo
ASP.NET MVC 5 JQuery DataTable Demo: `C#
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();
}
}
C#
@{
Layout = null;
}
AspNetMvcDemo
@Html.Raw(ViewBag.Table)
ASP.NET Core Demo:
C#
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Run(async (context) =>
{
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
await context.Response.WriteAsync(tablehtml);
});
}
}
---
Tranlated By Open Ai Tx | Last indexed: 2025-06-30
---