Dynamically create HTML table in C#

前端 未结 4 1532
逝去的感伤
逝去的感伤 2020-12-30 06:40

Is there more efficient way to build HTML table than the one I\'m trying on right now?

I\'m getting an object and it has some list of entities in it. So I need to pa

4条回答
  •  一个人的身影
    2020-12-30 07:18

    It is a decent approach, and just 'what it takes' to output something as complicated as HTML - unless you want to do it using plain strings (which is just as messy, if not worse).

    One improvement: do not use the same cell object multiple times, you run the risk of getting incorrect output. Improved code:

    row.Cells.Add(new HtmlTableCell { InnerText = invalidCompany.BusinessName });
    row.Cells.Add(new HtmlTableCell { InnerText = invalidCompany.SwiftBIC });
    row.Cells.Add(new HtmlTableCell { InnerText = invalidCompany.IBAN });
    

    Of course you can also create your own helpers for creating cells, for creating a row full of cells, etc. There are also good libraries for this, e.g. see https://www.nuget.org/packages/HtmlTags/.

提交回复
热议问题