Web Analytics

HtmlTableHelper

⭐ 42 stars Italian by mini-software

NuGet


version version version version version version version version version version version version version version version version version


Caratteristiche

Installazione

Puoi installare il pacchetto da NuGet utilizzando il Visual Studio Package Manager o l'interfaccia NuGet:

PM> install-package HtmlTableHelper

oppure tramite la riga di comando dotnet:

dotnet add package HtmlTableHelper

Demo Fiddle:

Per iniziare

##### Esempio di List/Array/Set/Enumerable di tipo non Chiave/Valore ``C# using HtmlTableHelper; .. var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } }; var tablehtml = sourceData.ToHtmlTable(); /* Risultato:

NameAgeGender
ITWeiHan25M
*/

##### Esempio Dapper
C# using (var cn = "Your Connection") { var sourceData = cn.Query(@"select 'ITWeiHan' Name,25 Age,'M' Gender"); var tablehtml = sourceData.ToHtmlTable(); }

##### Esempio Dictionary 
C# var sourceData = new[] {new Dictionary (){{"Name" , "ITWeiHan" },{"Age",25},{"Gender","M"}}}; var tablehtml = sourceData.ToHtmlTable();

Attributi personalizzati per Table/TR/TD/TH (Tipo Dinamico)

C# var data = /List/Array/Set/Enumerable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //questo è un tipo dinamico, supporta tutti gli attributi ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* Risultato:
..
..
*/

##### Attributi Annotazione

###### 1. Display :

C# public class ModelClassWithDisplayAttr { [TableColumn(DisplayName = "Colonna1")] //La proprietà MyProperty1 renderizzerà l'innertext di thead-td : "Colonna1" public string MyProperty1 { get; set; } [TableColumn(DisplayName = "Colonna2")] //La proprietà MyProperty2 renderizzerà l'innertext di thead-td : "Colonna2" public string MyProperty2 { get; set; } }

###### 2. Skip : 
C# public class ModelClassWithSkipAttr { [TableColumn( Skip = true)] public string MyProperty1 { get; set; } //MyProperty1 non renderizzerà html public string MyProperty2 { get; set; } }

##### HTMLTableBuilder

###### HtmlCaption

C# var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} }; var html = soucreData.CreateBuilder() .SetCaption("Questa è la Didascalia", new { id = "CaptionId" }) .ToHtmlTable(); //Risultato :
Questa è la Didascalia
MyProperty1MyProperty2
test123

##### HTMLTableSetting

Configurazione della codifica InnerHtml (Si consiglia di non farlo senza una ragione specifica, a causa di attacchi XSS)

C# var sourceData = new[] { new { Name = "ITWeiHan" } };

//Codifica di default var encodinghtml = sourceData.ToHtmlTable(); //Risultato:

..<b>ITWeiHan</b>..

var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //Risultato:

..ITWeiHan..

Estensione

ASP.NET Core MVC: Crea un file IHtmlHelperExtension.cs
C# using System.Collections.Generic; using HtmlTableHelper; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Html;

public static class IHtmlHelperExtension { public static HtmlString ToHtmlTable(this IHtmlHelper htmlHelper, IEnumerable 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(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 
C# @Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } }) /* Risultato:
NameAgeGender
ITWeiHan25M
*/

ASP.NET MVC 5:   
Crea un file HtmlHelperExtension.cs
C# using System.Collections.Generic; using HtmlTableHelper; using System.Web; using System.Web.Mvc;

public static class HtmlHelperExtension { public static HtmlString ToHtmlTable(this HtmlHelper htmlHelper, IEnumerable 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(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

Demo ASP.NET MVC 5 JQuery DataTable:
`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)

Demo ASP.NET Core:

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 ---