특징
- 작고(DLL 크기 20KB) 사용이 간편합니다.
- .NET Standard 2.0/.NET 4.6/.NET 4.5/.NET 4.0 지원
- 외부 라이브러리 불필요
- 익명 타입, Dapper 동적 쿼리, List/Array/Set/Enumerable, DataTable, Dictionary 지원
설치 방법
Visual Studio 패키지 관리자 또는 NuGet UI를 사용하여 NuGet에서 패키지를 설치할 수 있습니다:
PM> install-package HtmlTableHelper또는 dotnet 명령줄을 사용할 수 있습니다:
dotnet add package HtmlTableHelperFiddle 데모:
시작하기
##### List/Array/Set/Enumerable 키/값 타입이 아닌 예시
``C#
using HtmlTableHelper;
..
var sourceData = new[] { new { Name = "ITWeiHan", Age = "25",Gender = "M" } };
var tablehtml = sourceData.ToHtmlTable();
/*
결과:
*/
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 예시
C#
var sourceData = new[] {new Dictionary
##### Dictionary 예시
사용자 정의 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();
//결과 : | MyProperty1 | MyProperty2 |
|---|---|
| test | 123 |
##### HTMLTableSetting설정 가능한 InnerHtml 인코딩 (특별한 이유가 없다면 권장하지 않음, XSS 공격 위험)
C#
var sourceData = new[] { new { Name = "ITWeiHan" } };//기본 인코딩 var encodinghtml = sourceData.ToHtmlTable(); //결과:
var htmltablesetting = new HTMLTableSetting() { IsHtmlEncodeMode = false }; var notEncodinghtml = sourceData.ToHtmlTable(HTMLTableSetting: htmltablesetting); //결과:
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
public static HtmlString ToHtmlTable
C#
@Html.ToHtmlTable(new[] { new { Name = "ITWeiHan", Age = "25", Gender = "M" } })
/*
결과:
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:
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
데모
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
---