Web Analytics

HtmlTableHelper

⭐ 42 stars Korean by mini-software

NuGet


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


특징

설치 방법

Visual Studio 패키지 관리자 또는 NuGet UI를 사용하여 NuGet에서 패키지를 설치할 수 있습니다:

PM> install-package HtmlTableHelper

또는 dotnet 명령줄을 사용할 수 있습니다:

dotnet add package HtmlTableHelper

Fiddle 데모:

시작하기

##### List/Array/Set/Enumerable 키/값 타입이 아닌 예시 ``C# using HtmlTableHelper; .. var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } }; var tablehtml = sourceData.ToHtmlTable(); /* 결과:

NameAgeGender
ITWeiHan25M
*/

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

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

사용자 정의 Table/TR/TD/TH 속성 (동적 타입)

C# var data = /List/Array/Set/Enumrable../; var html = data.ToHtmlTable( tableAttributes: new { @class = "SomeClass"} //이것은 동적 타입이며, 모든 속성 지원 ,trAttributes: new { ID = "SomeID" },tdAttributes: new { width = "120 px" },thAttributes: new { @class = "dark-theme" } ); /* 결과:
..
..
*/

##### 속성 어노테이션

###### 1. Display :

C# public class ModelClassWithDisplayAttr { [TableColumn(DisplayName = "Column1")] //MyProperty1 속성은 thead-td의 innertext를 "Column1"로 렌더링합니다. public string MyProperty1 { get; set; } [TableColumn(DisplayName = "Column2")] //MyProperty2 속성은 thead-td의 innertext를 "Column2"로 렌더링합니다. public string MyProperty2 { get; set; } }

###### 2. Skip : 
C# public class ModelClassWithSkipAttr { [TableColumn( Skip = true)] public string MyProperty1 { get; set; } //MyProperty1은 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(); //결과 :
This is Caption
MyProperty1MyProperty2
test123

##### HTMLTableSetting

설정 가능한 InnerHtml 인코딩 (특별한 이유가 없다면 권장하지 않음, XSS 공격 위험)

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

//기본 인코딩 var encodinghtml = sourceData.ToHtmlTable(); //결과:

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

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

..ITWeiHan..

Extension

ASP.NET Core MVC: 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" } }) /* 결과:
NameAgeGender
ITWeiHan25M
*/

ASP.NET MVC 5:   
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); } }

데모

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)

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