- 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
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 HtmlTableHelperusingHtmlTableHelper;
..
var sourceData =new[]{new{Name="ITWeiHan",Age="25",Gender="M"}};vartablehtml=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>*/using(varcn="Your Connection"){varsourceData=cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender");vartablehtml=sourceData.ToHtmlTable();}varsourceData=new[]{newDictionary<string,object>(){{"Name","ITWeiHan"},{"Age",25},{"Gender","M"}}};vartablehtml=sourceData.ToHtmlTable();Custom Table/TR/TD/TH Attributes (Dynamic Type)
vardata=/*List/Array/Set/Enumrable..*/;varhtml=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>*/publicclassModelClassWithDisplayAttr{[TableColumn(DisplayName="Column1")]//MyProperty1 property will render thead-td's innertext : "Column1"publicstringMyProperty1{get;set;}[TableColumn(DisplayName="Column2")]//MyProperty2 property will render thead-td's innertext : "Column2"publicstringMyProperty2{get;set;}}publicclassModelClassWithSkipAttr{[TableColumn(Skip=true)]publicstringMyProperty1{get;set;}//MyProperty1 will not render htmlpublicstringMyProperty2{get;set;}}varsoucreData=new[]{new{MyProperty1="test",MyProperty2=123}};varhtml=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>Configurable InnerHtml Encoding (Recommended not to do so without a specific reason,because XSS Attack)
varsourceData=new[]{new{Name="<b>ITWeiHan</b>"}};//Default Encodingvarencodinghtml=sourceData.ToHtmlTable();//Result: <table>..<b>ITWeiHan</b>..</table>varhtmltablesetting=newHTMLTableSetting(){IsHtmlEncodeMode=false};varnotEncodinghtml=sourceData.ToHtmlTable(HTMLTableSetting:htmltablesetting);//Result: <table>..<b>ITWeiHan</b>..</table>ASP.NET Core MVC:
Create a IHtmlHelperExtension.cs
usingSystem.Collections.Generic;usingHtmlTableHelper;usingMicrosoft.AspNetCore.Mvc.Rendering;usingMicrosoft.AspNetCore.Html;publicstaticclassIHtmlHelperExtension{publicstaticHtmlStringToHtmlTable<T>(thisIHtmlHelperhtmlHelper,IEnumerable<T>enums,objecttableAttributes=null,objecttrAttributes=null,objecttdAttributes=null,HtmlTableSettingHTMLTableSetting=null){varhtml=enums.ToHtmlTable(tableAttributes,trAttributes,tdAttributes,HTMLTableSetting);returnnewHtmlString(html);}publicstaticHtmlStringToHtmlTable<T>(thisIHtmlHelperhtmlHelper,System.Data.DataTabledatatable,objecttableAttributes=null,objecttrAttributes=null,objecttdAttributes=null,HtmlTableSettingHTMLTableSetting=null){varhtml=datatable.ToHtmlTable(tableAttributes,trAttributes,tdAttributes,HTMLTableSetting);returnnewHtmlString(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
usingSystem.Collections.Generic;usingHtmlTableHelper;usingSystem.Web;usingSystem.Web.Mvc;publicstaticclassHtmlHelperExtension{publicstaticHtmlStringToHtmlTable<T>(thisHtmlHelperhtmlHelper,IEnumerable<T>enums,objecttableAttributes=null,objecttrAttributes=null,objecttdAttributes=null,HtmlTableSettingHTMLTableSetting=null){varhtml=enums.ToHtmlTable(tableAttributes,trAttributes,tdAttributes,HTMLTableSetting);returnnewHtmlString(html);}publicstaticHtmlStringToHtmlTable<T>(thisHtmlHelperhtmlHelper,System.Data.DataTabledatatable,objecttableAttributes=null,objecttrAttributes=null,objecttdAttributes=null,HtmlTableSettingHTMLTableSetting=null){varhtml=datatable.ToHtmlTable(tableAttributes,trAttributes,tdAttributes,HTMLTableSetting);returnnewHtmlString(html);}}ASP.NET MVC 5 JQuery DataTable Demo:
usingHtmlTableHelper;//..publicclassHomeController:Controller{publicActionResultIndex(){vardatas=new[]{new{Name="ITWeiHan",Age="25",Gender="M"}};ViewBag.Table=datas.ToHtmlTable();returnView();}}@{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.min.js"></script><script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script><script>
$(document).ready(function(){
$('Table').DataTable();});</script></body></html>ASP.NET Core Demo:
publicclassStartup{publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){app.Run(async(context)=>{varsourceData=new[]{new{Name="ITWeiHan",Age="25",Gender="M"}};vartablehtml=sourceData.ToHtmlTable();awaitcontext.Response.WriteAsync(tablehtml);});}}