【发布时间】:2021-11-14 00:50:15
【问题描述】:
我有一个从 DataTable 创建 html 表的函数。我附上了下面的代码。现在我的问题是,如何在 Blazor 应用程序中将字符串转换为 HTML 代码?
感谢您的努力。 :)
public static string ConvertDataTableToHtml(DataTable dt)
{
string html = "<table>";
//add header row
html += "<tr>";
for (int i = 0; i < dt.Columns.Count; i++)
html += "<td>" + dt.Columns[i].ColumnName + "</td>";
html += "</tr>";
//add rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < dt.Columns.Count; j++)
html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html;
}
【问题讨论】:
-
根本不要创建这样的字符串。请改用 Razor 语法。您当前的代码甚至没有生成有效的 HTML - 该 DataTable 中的任何特殊字符或恶意脚本内容最终都会出现在最终的 HTML 中。充其量,其中一个单元格中的
<或>最终会弄乱表格。最坏的情况是,存储在表中的任何 Javascript 都会被执行 -
好的,我会试试的,谢谢。
-
当所有 ASP.NET Core 示例都使用 Razor 时,为什么你首先尝试创建一个字符串?
标签: html datatable blazor converters