Web Analytics

HtmlTableHelper

⭐ 42 stars German by mini-software

NuGet


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


Funktionen

Installation

Sie können das Paket über NuGet mit dem Visual Studio Paket-Manager oder der NuGet-Benutzeroberfläche installieren:

PM> install-package HtmlTableHelper

oder über die dotnet Befehlszeile:

dotnet add package HtmlTableHelper

Fiddle Demo:

Einstieg

##### List/Array/Set/Enumerable Beispiel für Nicht-Key/Value-Typen ``C# using HtmlTableHelper; .. var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } }; var tablehtml = sourceData.ToHtmlTable(); /* Ergebnis:

NameAgeGender
ITWeiHan25M
*/

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

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

Benutzerdefinierte Table/TR/TD/TH Attribute (Dynamischer Typ)

C# var data = /List/Array/Set/Enumerable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //dies ist ein dynamischer Typ, unterstützt alle Attribute ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* Ergebnis:
..
..
*/

##### Attribut-Anmerkung

###### 1. Display :

C# public class ModelClassWithDisplayAttr { [TableColumn(DisplayName = "Spalte1")] //MyProperty1-Eigenschaft rendert thead-td's innertext : "Spalte1" public string MyProperty1 { get; set; } [TableColumn(DisplayName = "Spalte2")] //MyProperty2-Eigenschaft rendert thead-td's innertext : "Spalte2" public string MyProperty2 { get; set; } }

###### 2. Überspringen : 
C# public class ModelClassWithSkipAttr { [TableColumn( Skip = true)] public string MyProperty1 { get; set; } //MyProperty1 wird kein HTML rendern public string MyProperty2 { get; set; } }

##### HTMLTableBuilder

###### HtmlCaption

C# var soucreData = new []{ new {MyProperty1="test",MyProperty2=123} }; var html = soucreData.CreateBuilder() .SetCaption("Dies ist die Beschriftung", new { id = "CaptionId" }) .ToHtmlTable(); //Ergebnis :
Dies ist die Beschriftung
MyProperty1MyProperty2
test123

##### HTMLTableSetting

Konfigurierbare InnerHtml-Kodierung (Wird ohne bestimmten Grund nicht empfohlen, da XSS-Angriff möglich)

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

//Standardmäßig kodiert var encodinghtml = sourceData.ToHtmlTable(); //Ergebnis:

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

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

..ITWeiHan..

Erweiterung

ASP.NET Core MVC: Erstellen Sie eine 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" } }) /* Ergebnis:
NameAgeGender
ITWeiHan25M
*/

ASP.NET MVC 5:   
Erstellen Sie eine 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

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